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

Bad projection with findAndModify()

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • None
    • 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

    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

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: