I have one collection called Resources with documents inside:
{
"_id" : "fGR4ECcw5p2HFgxd3",
"ownerId" : "J8MpsWChPQdET6jwQ",
"inventory" : [
{
"_id" : ObjectId("5b537ef5895adfeae037e0a4"),
"quantity" : 17
},
{
"_id" : ObjectId("5b537d9c895adfeae037dbd2"),
"quantity" : 6
},
{
"_id" : ObjectId("5b5868e081702f675304290e"),
"quantity" : 26
},
{
"_id" : ObjectId("5b5eba8276dd90bd75e27f13"),
"quantity" : 85
},
{
"_id" : ObjectId("5b5ee58176dd90bd75e2f35f"),
"quantity" : 5
},
{
"_id" : ObjectId("5b62275686f92e050a9d50b2"),
"quantity" : 3
}
]
}
What i am trying to do is get object of inventory array with that objects id
I feel like this should work but its not
Resources.findOne({ownerId:userid,"inventory._id":ObjectId})
my expected output for example:
{
"_id" : ObjectId("5b537ef5895adfeae037e0a4"),
"quantity" : 17
}
I also tried this:
console.log(Resources.findOne({ownerId:userid},{fields:{inventory:{$elemMatch:{_id:ids[i]}}}}));