I want to find a single element from embedded document in MongoDB. Here are my documents of user collection in MongoDB:-
[
{
"userId":"001",
"userName":"abc"
"subscriptionDetails" : [
{
"planId" : "1",
"isExpired" : false,
"current" : true,
"timeStamp" : 1592570927981,
},
{
"planId" : "2",
"isExpired" : false,
"current" : true,
"timeStamp" : 1592570927909,
},
{
"planId" : "3",
"isExpired" : false,
"current" : false,
"timeStamp" : 1592570978,
}
]
},
{
"userId":"002",
"userName":"abc"
"subscriptionDetails" : [
{
"planId" : "1",
"isExpired" : false,
"current" : true,
"timeStamp" : 1592570927981,
}
]
}
]
My nodejs function to find the single element from embedded documents by nodejs native driver:-
var Plan=await db.collection("user").findOne(
{
userId:001,
'subscriptionDetails.current':false,
'subscriptionDetails.isExpired':false
},
{
'projection':{ "_id": 0,'subscriptionDetails.$':1 }
}
)
console.log(plan)
But the response of this function is:-
{
subscriptionDetails:
[ {
"planId" : "1",
"isExpired" : false,
"current" : true,
"timeStamp" : 1592570927981,
}
]
}
But the expected output is:-
{
subscriptionDetails:
[ {
"planId" : "3",
"isExpired" : false,
"current" : false,
"timeStamp" : 1592570978,
}
]
}
How can I solve this?