I am trying to get a specific field from an array of subdocuments
I will not include any fields in the parent document
Here is a sample document
{ "_id" : ObjectId("5409dd36b71997726532012d"), "hierarchies" : [ { "rank" : 1, "_id" : ObjectId("5409df85b719977265320137"), "name" : "CTO", "userId" : [ ObjectId("53a47a639c52c9d83a2d71db") ] } ] }
I would like to return the rank of the hierarchy if userId is in the userId array
here is what i still have in my request
collectionName.find({{hierarchies: {$elemMatch : {userId: ObjectId("53a47a639c52c9d83a2d71db")}}} , "hierarchies.$.rank", function(err,data){}
still it returns the whole object in the hierarchy array that I want, but I would like to limit it to only the object's rank property.
source share