You need $
operator in this way:
db.collection.find({
"totalCreatedForms.formName": "formtwo"
},
{
"_id": 0,
"totalCreatedForms.formIndex.$": 1
})
Find query has two objects.
The first to match the element you are looking for.
The second one is to indicate which fields returns. So, _id
field will not be returned and, from array, only the index where the positional operator is placed, will be returned.
Ouput example is like this:
[
{
"totalCreatedForms": [
{
"formIndex": 1
}
]
}
]
Note that mongo is a data base who store documents and return documents, so this is the easiest way (without aggregation) to get only the value you want instead of the entire array.
Example here
CLICK HERE to find out more related problems solutions.