Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-7421

Using the projection $elemMatch causes all other fields not to be selected in query response

    • Query Execution
    • ALL

      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.

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            ajay.s Ajay Sabhaney
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: