[SERVER-10577] Updating sub document with positional operator Created: 20/Aug/13  Updated: 11/Jul/16  Resolved: 22/Oct/13

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.2.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Mayur Kataria Assignee: David Hows
Resolution: Done Votes: 0
Labels: update
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 12.10,


Operating System: ALL
Steps To Reproduce:

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 }

} );

Participants:

 Description   

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.



 Comments   
Comment by David Hows [ 22/Oct/13 ]

Thanks Mayur,

As you have resolved this issue I'm marking this ticket as closed.

Regards,
David

Comment by Mayur Kataria [ 21/Oct/13 ]

Thanks David,

I've already solved the same. The problem is as end user, i thought of applying simple update query
db.students.update( {'$and':[

{ _id: 4}

,

{ "grades.grade": 85 }

,

{"grades.std": 5 }

]}, { $set:{ "grades.$.std" : 6 }} );

It's not mentioned in the document for update that "while using Update & 'and' condition you should use $elemMatch in case of subdocument."

As end user, i thought like that.

Comment by David Hows [ 21/Oct/13 ]

Hi Mayur,

You should use the $elemMatch operator, this will allow you to match a specific sub-document with multiple criteria, rather than one criteria. Eg

db.students.update({ _id: 4, grades: { $elemMatch:{ grade: 85, std: 5} }},{$set : {"grades.$.std": 6}});

Does this fit your usecase?

Regards,
David

Generated at Thu Feb 08 03:23:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.