-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 2.2.0
-
Component/s: Querying
-
Query Execution
-
ALL
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Currently, using the $elemMatch projection as a part of the field selection causes all other fields in the collection not to be included in the query response.
For example, given the following document:
{
_id: ObjectId(),
name: "Sample",
image: "sample.jpg",
members: [
{ name: "John", city: "Calgary" },
{ name: "Gerry", city: "Berlin" }]
}
The query
db.projects.find({}, {dependents: { $elemMatch: { city: "Calgary" } } });
would yield the following result:
[{
_id: ObjectId(),
members: [ { name: "John", city: "Calgary" } ]
}]
There should be a way to include all fields to be selected (perhaps by default).
In the MongoDB document titled "Retrieving a Subset of Fields", it mentions that "Filtering with $slice does not affect other fields inclusion/exclusion. It only applies within the array being sliced." It would be appropriate if this same logic is applied to the $elemMatch projection. Also, the documentation should be adjusted to reflect the field inclusion/exclusion behaviour.