Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Problem: Given the following document in the database, I want to update
# Dave Gahan's second prescription for Dilaudid from a quantity of 45 to
# a quantity of 30.
{ "_id" : 1
"name" : "Dave Gahan"
"medications" : [
{ "id" : 23,
"name" : "Dilaudid",
"type" : "Rx",
"prescriptions" : [
{ "id" : 13,
"quantity" : 60,
"started" : 2009-01-01
},
{ "id" : 77,
"quantity" : 45,
"started" : 2009-02-01
}
]
},
{ "id" : 41,
"name" : "Oxycodone",
"type" : "Rx"
}
]
}
# First try was using 1.3.4 $ positional locator... These do not find the
# document embedded multiple levels:
collection.update(
{ "_id" : 1, "medications._id" : 23, "medications.prescriptions._id" : 77 },
{ $set : { "medications.prescriptions.$.quantity" : 30 } },
false,
true
)
collection.update(
{ "_id" : 1, "medications._id" : 23, "medications.prescriptions._id" : 77 },
{ $set : { "medications.$.prescriptions.$.quantity" : 30 } },
false,
true
)
# What I would like is to be able to match on mutliples, given a syntax like the following:
collection.update(
{ "_id" : 1, "medications._id" : 23, "medications.prescriptions._id" : 77 },
{ $set : { "medications.$0.prescriptions.$1.quantity" : 30 } },
false,
true
)
@rdetert

This comment has been minimized.

Show comment Hide comment
@rdetert

rdetert Aug 14, 2010

Any luck with this?

rdetert commented Aug 14, 2010

Any luck with this?

@Blurjp

This comment has been minimized.

Show comment Hide comment
@Blurjp

Blurjp Sep 27, 2012

not yet

Blurjp commented Sep 27, 2012

not yet

@tejasshah93

This comment has been minimized.

Show comment Hide comment
@tejasshah93

tejasshah93 Dec 6, 2013

Got any solution for this? I'm stuck at the same point..

Got any solution for this? I'm stuck at the same point..

@ovinokurov

This comment has been minimized.

Show comment Hide comment
@ovinokurov

ovinokurov Feb 14, 2014

setModifier.$set['medications.$'] = JSON.parse({ "id" : 77, "quantity" : 45, "started" : 2009-02-01});

db.DBname.update({'_id': 1,"medications.id":77}, setModifier, function(err, updated) {
if( err || !updated ) console.log("Error: "+err);
else console.log("updated");
});
} catch (e) {
console.log("Error");
console.log(e);
}

setModifier.$set['medications.$'] = JSON.parse({ "id" : 77, "quantity" : 45, "started" : 2009-02-01});

db.DBname.update({'_id': 1,"medications.id":77}, setModifier, function(err, updated) {
if( err || !updated ) console.log("Error: "+err);
else console.log("updated");
});
} catch (e) {
console.log("Error");
console.log(e);
}

@thalesfsp

This comment has been minimized.

Show comment Hide comment
@thalesfsp

thalesfsp Mar 25, 2014

Its working?

setModifier.$set['medications.$'] = JSON.parse({ "id" : 77, "quantity" : 45, "started" : 2009-02-01});

db.DBname.update({'_id': 1,"medications.id":77}, setModifier, function(err, updated) {
if( err || !updated ) console.log("Error: "+err);
else console.log("updated");
});
} catch (e) {
console.log("Error");
console.log(e);
}

Its working?

setModifier.$set['medications.$'] = JSON.parse({ "id" : 77, "quantity" : 45, "started" : 2009-02-01});

db.DBname.update({'_id': 1,"medications.id":77}, setModifier, function(err, updated) {
if( err || !updated ) console.log("Error: "+err);
else console.log("updated");
});
} catch (e) {
console.log("Error");
console.log(e);
}

@LoicSellaye

This comment has been minimized.

Show comment Hide comment
@LoicSellaye

LoicSellaye May 19, 2014

Hello everybody,

I am really interested about finding the solution of this problem because I face the same problem like you.

Best
Loic

Hello everybody,

I am really interested about finding the solution of this problem because I face the same problem like you.

Best
Loic

@xhh261672

This comment has been minimized.

Show comment Hide comment
@xhh261672

xhh261672 Sep 9, 2014

Stuck,too. Waiting for solutions
best wishes~~

Stuck,too. Waiting for solutions
best wishes~~

@somethvictory

This comment has been minimized.

Show comment Hide comment
@somethvictory

somethvictory Oct 30, 2014

4 years till now, is there any luck with this?

4 years till now, is there any luck with this?

@thatisuday

This comment has been minimized.

Show comment Hide comment
@thatisuday

thatisuday Nov 5, 2014

It works with "medications.$.prescriptions.0.quantity" : 30,
which means mongodb is unable to find position of nested document...

It works with "medications.$.prescriptions.0.quantity" : 30,
which means mongodb is unable to find position of nested document...

@kundansharma

This comment has been minimized.

Show comment Hide comment
@kundansharma

kundansharma Jan 3, 2015

Try this... I hope this solution will help you...

collection.aggregate({$match:{_id:1}},{$unwind:'$medications'},{ $match: { 'medications.id' : 23} },function(err,result1){
if(err){
throw(err);
}else{
indexes = result1[0].medications.prescriptions.map(function(obj, index) {
if(obj.id == 13) {
return index;
}
}).filter(isFinite);
var updateData = {};
updateData["medications.$.prescriptions."+indexes[0]+".quantity"]=30;
collection.update({ _id:1,'medications.id': 23,'medications.prescriptions.id': 13 },{$set: updateData },function(err,result2){
console.log(result2);
});
}
});

Try this... I hope this solution will help you...

collection.aggregate({$match:{_id:1}},{$unwind:'$medications'},{ $match: { 'medications.id' : 23} },function(err,result1){
if(err){
throw(err);
}else{
indexes = result1[0].medications.prescriptions.map(function(obj, index) {
if(obj.id == 13) {
return index;
}
}).filter(isFinite);
var updateData = {};
updateData["medications.$.prescriptions."+indexes[0]+".quantity"]=30;
collection.update({ _id:1,'medications.id': 23,'medications.prescriptions.id': 13 },{$set: updateData },function(err,result2){
console.log(result2);
});
}
});

@whilda

This comment has been minimized.

Show comment Hide comment
@whilda

whilda Jan 10, 2015

There are any solution for this problem if i work with java?

whilda commented Jan 10, 2015

There are any solution for this problem if i work with java?

@shibindot

This comment has been minimized.

Show comment Hide comment
@shibindot

shibindot Apr 4, 2016

for the below aggregation query it results error

assert: command failed: {
"ok" : 0,
"errmsg" : "pipeline element 3 is not an object",
"code" : 15942
} : aggregate failed

db.country_state_city.aggregate({$unwind:'$stateinfo'},{$unwind:'$stateinfo.cityinfo'},
{ $match: {$and:[{"_id":1},{ "stateinfo.state_id" : 1}]}},function(err,result1){
if(err){
throw(err);
}else{
indexes = result1[0].stateinfo.cityinfo.map(function(obj,index)
{
if(obj.city_id == 3) {
return index;
}
}).filter(isFinite);
var updateData = {};
updateData["stateinfo.$.cityinfo."+indexes[0]+".default"]=120;
db.country_state_city.update({ "_id":1,"stateinfo.state_id": 1,"stateinfo.cityinfo.city_id": 3 },
{$set: updateData },function(err,result2){
console.log(result2);
});
}

for the below aggregation query it results error

assert: command failed: {
"ok" : 0,
"errmsg" : "pipeline element 3 is not an object",
"code" : 15942
} : aggregate failed

db.country_state_city.aggregate({$unwind:'$stateinfo'},{$unwind:'$stateinfo.cityinfo'},
{ $match: {$and:[{"_id":1},{ "stateinfo.state_id" : 1}]}},function(err,result1){
if(err){
throw(err);
}else{
indexes = result1[0].stateinfo.cityinfo.map(function(obj,index)
{
if(obj.city_id == 3) {
return index;
}
}).filter(isFinite);
var updateData = {};
updateData["stateinfo.$.cityinfo."+indexes[0]+".default"]=120;
db.country_state_city.update({ "_id":1,"stateinfo.state_id": 1,"stateinfo.cityinfo.city_id": 3 },
{$set: updateData },function(err,result2){
console.log(result2);
});
}

@stanisdev

This comment has been minimized.

Show comment Hide comment
@stanisdev

stanisdev May 14, 2016

This solution (medications.$.prescriptions.0.quantity) was helpful, but arises next issue: to find index of array (in example is used "0"). As version of solution to find and retrieve index of necessary element in array as separate query and substitute inside "update query".
Any way, thank you for advice.

This solution (medications.$.prescriptions.0.quantity) was helpful, but arises next issue: to find index of array (in example is used "0"). As version of solution to find and retrieve index of necessary element in array as separate query and substitute inside "update query".
Any way, thank you for advice.

@jonathanmccormick

This comment has been minimized.

Show comment Hide comment
@jonathanmccormick

jonathanmccormick Jul 5, 2016

I don't know if there's a reason MongoDB doesn't support this, but boy would it make life so much easier...

I don't know if there's a reason MongoDB doesn't support this, but boy would it make life so much easier...

@dlee148

This comment has been minimized.

Show comment Hide comment
@dlee148

dlee148 Aug 2, 2016

It's 2016 and this is very much still an issue. I have created a simple workaround using the C# driver, hopefully it helps.
https://gist.github.com/dlee148/a0ec830baff5c71471213616f22cb902

dlee148 commented Aug 2, 2016

It's 2016 and this is very much still an issue. I have created a simple workaround using the C# driver, hopefully it helps.
https://gist.github.com/dlee148/a0ec830baff5c71471213616f22cb902

@Guihgo

This comment has been minimized.

Show comment Hide comment
@Guihgo

Guihgo Jan 13, 2017

any updates ?? *2017

Guihgo commented Jan 13, 2017

any updates ?? *2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment