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

Bad projection with findAndModify()

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      My failing test :

      My object from my collection 'tests':

      {
          "_id" : ObjectId("56e81455d98ae5bedd005583"),
          "array" : [
              {
                  "field1": "e1f1",
                  "field2" : "e1f2",
              },
              {
                  "field1": "e2f1",
                  "field2": "e2f2",
              },
          ],
      }
      

      > db.tests.findAndModify({ query: { _id: ObjectId('56e81455d98ae5bedd005583'), array: { '$elemMatch': { field1: "e2f1" } } }, update: { $set: { 'array.$.field2': 'e2f2' } }, new: false, upsert: false, fields: { 'array.$': 1 } })
      {
          "_id" : ObjectId("56e81455d98ae5bedd005583"),
          "array" : [
              {
                  
              },
              {
                  
              }
          ]
      }
      

      The result returns all elements of "array" with empty values.

      > db.tests.find({ _id: ObjectId('56e81455d98ae5bedd005583'), array: { '$elemMatch': { field1: "e2f1" } } }, { 'array.$': 1 })
      { "_id" : ObjectId("56e81455d98ae5bedd005583"), "array" : [ { "field1" : "e2f1", "field2" : "e2f2" } ] }
      

      The operation above returns the expected result, but with findAndModify() the result is not the same.

      $ mongo --version
      MongoDB shell version: 3.0.3
      

      Show
      My failing test : My object from my collection 'tests': { "_id" : ObjectId("56e81455d98ae5bedd005583"), "array" : [ { "field1": "e1f1", "field2" : "e1f2", }, { "field1": "e2f1", "field2": "e2f2", }, ], } > db.tests.findAndModify({ query: { _id: ObjectId('56e81455d98ae5bedd005583'), array: { '$elemMatch': { field1: "e2f1" } } }, update: { $set: { 'array.$.field2': 'e2f2' } }, new: false, upsert: false, fields: { 'array.$': 1 } }) { "_id" : ObjectId("56e81455d98ae5bedd005583"), "array" : [ { }, { } ] } The result returns all elements of "array" with empty values. > db.tests.find({ _id: ObjectId('56e81455d98ae5bedd005583'), array: { '$elemMatch': { field1: "e2f1" } } }, { 'array.$': 1 }) { "_id" : ObjectId("56e81455d98ae5bedd005583"), "array" : [ { "field1" : "e2f1", "field2" : "e2f2" } ] } The operation above returns the expected result, but with findAndModify() the result is not the same. $ mongo --version MongoDB shell version: 3.0.3

      Description

      I try to query, update and project the first element matching the query document with findAndModify(), but the result returns all elements with empty values.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: