[SERVER-7421] Using the projection $elemMatch causes all other fields not to be selected in query response Created: 19/Oct/12  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: 2.2.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Ajay Sabhaney Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 1
Labels: mql-semantics, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Execution
Operating System: ALL
Participants:

 Description   

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.



 Comments   
Comment by Patrick Grandjean [ 22/Nov/13 ]

+1000

Generated at Thu Feb 08 03:14:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.