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

Bad projection with findAndModify()

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • ALL
    • 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

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            alexandre-ezeeworld Alexandre
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: