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

Updating sub document with positional operator

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.2
    • Component/s: Write Ops
    • Environment:
      Ubuntu 12.10,
    • ALL
    • Hide

      db.students.remove();

      db.students.insert( { "_id" : 4, "grades" : [

      { grade: 80, mean: 75, std: 8 }

      ,

      { grade: 85, mean: 90, std: 5 }

      ,

      { grade: 85, mean: 90, std: 5 }

      ,

      { grade: 85, mean: 95, std: 6 }

      ,

      { grade: 90, mean: 85, std: 5 }

      ] });

      db.students.update( {'$and':[

      { _id: 4}

      ,

      { "grades.grade": 85 }

      ,

      {"grades.std": 5 }

      ]}, { $set:

      { "grades.$.std" : 6 }

      } );

      Show
      db.students.remove(); db.students.insert( { "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 } , { grade: 85, mean: 90, std: 5 } , { grade: 85, mean: 90, std: 5 } , { grade: 85, mean: 95, std: 6 } , { grade: 90, mean: 85, std: 5 } ] }); db.students.update( {'$and':[ { _id: 4} , { "grades.grade": 85 } , {"grades.std": 5 } ]}, { $set: { "grades.$.std" : 6 } } );

      Hi,

      Please find my mongodb version details as follows.

      db version v2.2.2, pdfile version 4.5
      Tue Aug 20 15:15:26 git version: d1b43b61a5308c4ad0679d34b262c5af9d664267

      Problem:

      let's say i have student collection and inserting the value as follows.

      db.students.insert(

      { "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 }

      ,

      { grade: 85, mean: 90, std: 5 }

      ,

      { grade: 85, mean: 90, std: 5 }

      ,

      { grade: 85, mean: 95, std: 6 }

      ,

      { grade: 90, mean: 85, std: 5 }

      ]
      }
      );

      i've one requirement where i want to update all the sub documents which is having id=4, grades.grade=85 and grades.std=5

      so following is my update statement which i am executing multiple times because "$" operator works only for first matching sub document.

      db.students.update( {'$and':[

      { _id: 4}

      ,

      { "grades.grade": 85 }

      ,

      {"grades.std": 5 }

      ]}, { $set:

      { "grades.$.std" : 6 }

      } );

      But instead of updating 2nd & 3rd subdocument only , its applying "or" condition like either grades.grade = 85 or grades.std = 5

      The Last sub document also getting updated.I feel its bug.Correct me if i am wrong.

            Assignee:
            david.hows David Hows
            Reporter:
            mayurck291 Mayur Kataria
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: