You could try something like this (It will be great, but... look at question update part):
db.coll.update(
{ company: "x company", office: "y office", "subOrders.products.productId": "z id"},
{ $set: { "subOrders.$.products.$.status": "delivered" } }
)
Also you could read this documentation about update operations: http://docs.mongodb.org/manual/reference/method/db.collection.update/
Update:
Ops.. Sorry, but positional operator ($
) don't work with nested arrays... You can see it on JIRA and also in documentation.
Some related questions: 1, 2
You can use "subOrders.$.products.0.status"
instead of "subOrders.$.products.$.status"
if it is possible in your case. But it will be better if you add some changes to your schema and get rid of the need to update documents in nested arrays.
But in general update syntax looks like as shown earlier.
Update 2:
For find only one document and update status
of only one product in only one subOrder you could do something like this:
var productIdForUpdate = "z id";
var doc = db.coll.findOne({ company: "x company", office: "y office", "subOrders.products.productId": productIdForUpdate });
top:
for(var i = 0; i < doc.subOrders.length; i++) {
var subOrder = doc.subOrders[i];
for(var j = 0; j < subOrder.products.length; j++) {
var product = subOrder.products[j];
if (product.productId == productIdForUpdate) {
eval('db.coll.update({ _id: doc._id }, { $set: { "subOrders.' + i + '.products.' + j + '.status": "delivered" } } )');
break top;
}
}
}
or:
var productIdForUpdate = "z id";
var doc = db.coll.findOne({ company: "x company", office: "y office", "subOrders.products.productId": productIdForUpdate });
top:
for(var i = 0; i < doc.subOrders.length; i++) {
var subOrder = doc.subOrders[i];
for(var j = 0; j < subOrder.products.length; j++) {
var product = subOrder.products[j];
if (product.productId == productIdForUpdate) {
product.status = "delivered";
db.coll.save(doc);
break top;
}
}
}
Shown code updating only one product with particular index in only one subOrder. If you want to update more than one document or more than one subOrder... or more than one product, you should add some changes to this code.