Considering:
{
docs: Array[{field1, field2}]
}
I know how to find the document(s) containing field1
or field2
with $elemMatch
, but my question is how can I get the value of field1 knowing field2?
I also know that in MongoDB you would use $elemMatch
in the projection parameter. Or that I can do this in JS with something like _.find()
, but the goal is to get only one specific document from Mongo.
Let say for example a document:
{
_id: 1
docs: [{a: 42, b:"stringX"}, {a:0, b:"stringY"}]
}
How can I get the value of a (42) knowing b ("stringX")?
Is there something like: MyCollection.findOne({_id: 1}, projection: {docs:{b: "stringX"}}) ?