I am trying to update multiple subdocuments within a Mongo collection, where there may be multiple matches within the same collection. Unfortunately, it is only updating the first subdocument within each document that has a match.
Here is the code:
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/touchbits');
var PartsSchema = new mongoose.Schema({
type: String
, partNbr: Number
});
var ProductSchema = new mongoose.Schema({
sku: { type: String, unique: true }
, parts: [PartsSchema]
});
Product = mongoose.model('Product', ProductSchema);
var cigars = new Product({
sku: 'cigar123',
parts: [{type: 'tobacco', partNbr: 4},
{type: 'rolling paper', partNbr: 8},
{type: 'tobacco', partNbr: 4}]
});
var cigarillo = new Product({
sku: 'cigarillo456',
parts: [{type: 'tobacco', partNbr: 4},
{type: 'crush paper', partNbr: 12}]
});
cigars.save(function(err, product1) {
if(err) { console.log("err1: " + err); return err; }
console.log("saved: " + product1);
cigarillo.save(function(err, product2) {
if(err){ console.log("err2: " + err); return err; }
console.log("saved: " + product2);
Product.update({ "parts.type": 'tobacco' },
{ $set: { "parts.$.partNbr": 5 } },
{ multi: true },
function(err, numAffected) {
if (err) { console.log("err3: " + err); return err; }
console.log("records updated: " + numAffected);
Product.find({}, function(err, docs) {
if (err) { console.log("err4: " + err); return err; }
console.log("updated data: " + docs);
Product.remove(function(err) {
if (err) { console.log("err5: " + err); return err; }
process.exit();
});
});
});
});
});
Then when I run it, I get the following results:
saved: { __v: 0,
sku: 'cigar123',
_id: 51c9229b7fe80ef71e000002,
parts:
[ { type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000005 },
{ type: 'rolling paper',
partNbr: 8,
_id: 51c9229b7fe80ef71e000004 },
{ type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000003 } ] }
saved: { __v: 0,
sku: 'cigarillo456',
_id: 51c9229b7fe80ef71e000006,
parts:
[ { type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000008 },
{ type: 'crush paper',
partNbr: 12,
_id: 51c9229b7fe80ef71e000007 } ] }
records updated: 2
updated data: { sku: 'cigar123',
_id: 51c9229b7fe80ef71e000002,
__v: 0,
parts:
[ { type: 'tobacco', partNbr: 5, _id: 51c9229b7fe80ef71e000005 },
{ type: 'rolling paper',
partNbr: 8,
_id: 51c9229b7fe80ef71e000004 },
{ type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000003 } ] },{ sku: 'cigarillo456',
_id: 51c9229b7fe80ef71e000006,
__v: 0,
parts:
[ { type: 'tobacco', partNbr: 5, _id: 51c9229b7fe80ef71e000008 },
{ type: 'crush paper',
partNbr: 12,
_id: 51c9229b7fe80ef71e000007 } ] }
Notice that the second 'tobacco' record in the 'cigars' document still has a PartNbr of 4 instead of 5. Does anyone know how to change the update to update all the subdocuments within an array that match the query condition?
The feature to update multiple sub documents within one request like that has not yet been implemented.
It has a current request at https://jira.mongodb.org/browse/SERVER-1243