目前 mongo 仅支持更新与指定 c 匹配的数组中的单个子文档。这非常适用于具有唯一标识符(例如_id)的子文档,您正在根据该标识符进行更新,但当有多个符合条件的子文档(例如学生年龄)时不起作用。
为了证明这一点,我在第一组学生中添加了一个学生“dupey”,因此新记录如下所示:
students: [
{ name: "john", school: 102, age: 4 },
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 7 },
{ name: "dupey", school: 102, age: 7 }
]
下面是mongo更新语句和更新完成后的数据结果:
> db.students.update({"students.age":7},{$set: {"students.$.school":101}},true,true);
> db.students.find({}, { _id: 0 });
{ "students" : [ { "name" : "john", "school" : 102, "age" : 4 }, { "name" : "jess", "school" : 102, "age" : 11 }, { "name" : "jeff", "school" : 101, "age" : 7 }, { "name" : "dupey", **"school" : 102**, "age" : 7 } ] }
{ "students" : [ { "name" : "ajax", "school" : 101, "age" : 7 }, { "name" : "achilles", "school" : 100, "age" : 8 } ] }
{ "students" : [ { "name" : "bob", "school" : 101, "age" : 7 }, { "name" : "manuel", "school" : 100, "age" : 8 } ] }
注意 dupey 的学校没有改变。有一个 mongo 功能请求来支持这一点。在此之前,必须将其分解为多个语句。这是我几周前遇到类似问题时写的一些代码。
var mongoose = require('mongoose'),
async = require('async');
mongoose.connect('mongodb://127.0.0.1/products');
var PartsSchema = new mongoose.Schema({
type: String
, partNbr: Number
});
var ProductSchema = new mongoose.Schema({
sku: { type: String, unique: true }
, sku_type: String
, parts: [PartsSchema]
});
Product = mongoose.model('Product', ProductSchema);
Product.remove( function(err) {
if (err) { console.log("unable to remove: " + err); return; }
});
var cigars = new Product({
sku: 'cigar123',
sku_type: 'smoking',
parts: [{type: 'tobacco', partNbr: 4}, {type: 'rolling paper', partNbr: 8}, {type: 'tobacco', partNbr: 4}]
});
var cigarillo = new Product({
sku: 'cigarillo456',
sku_type: 'smoking',
parts: [{type: 'tobacco', partNbr: 4}, {type: 'crush paper', partNbr: 12}]
});
function updateProducts(sku_type, part_type, callback) {
function updateProduct(product_record, callback) {
function denormParts(part_records, sku_id) {
var returnArray = [];
part_records.forEach( function(item) {
var record = { sku_id: sku_id, part_type: item.type, part_id: item._id }
returnArray.push(record);
});
return returnArray;
function updateParts(part_record, callback) {
if (part_record.part_type != part_type) {
return callback(null);
}
console.log("updating based on: " + part_record.sku_id + " " + part_record.part_id);
Product.update( { _id: part_record.sku_id, 'parts._id': part_record.part_id },
{ $set: { 'parts.$.partNbr': 5 } },
function(err, numAffected) {
if (err) { console.log("err4: " + err); return callback(err); }
console.log("records updated: " + numAffected);
callback(null);
});
}
var denormedParts = denormParts(product_record.parts, product_record._id);
console.log("calling updateParts with: " + JSON.stringify(denormedParts));
async.map(denormedParts, updateParts, function(err) {
if (err) { console.log("errored out: " + err); callback(err); }
callback(null);
});
}
Product.find({ "parts.type": part_type, "sku_type": sku_type },
function(err, docs) {
if (err) { console.log("err3: " + err); return callback(err); }
console.log(docs);
async.map(docs, updateProduct, function(err) {
if (err) { console.log("err4: " + err); return callback(err); }
callback(null);
});
}); // end Product.find
}
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);
updateProducts('smoking', 'tobacco', function(err) {
Product.find({}, function(err, docs) {
if (err) { console.log("err5: " + err); return err; }
console.log("updated data: " + docs);
Product.remove(function(err) {
if (err) { console.log("err6: " + err); return err; }
process.exit();
});
});
});
});
});
我希望这段代码可以帮助你和其他遇到这个问题的人。