我有这样的数据结构:
{
students: [
{ name: "john", school: 102, age: 4 },
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 7 }
]
}
{
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
students: [
{ name: "bob", school: 100, age: 7 },
{ name: "manuel", school: 100, age: 8 },
]
}
我想更新文件,以便任何7岁的学生改为101学校。有没有办法在1次操作中做到这一点?
答案 0 :(得分:1)
目前,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 update语句和更新完成后的数据结果:
> 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();
});
});
});
});
});
我希望此代码可以帮助您和遇到此问题的其他人。
答案 1 :(得分:0)
您可以通过指定其位置来更新数组,例如:
db.students.update({"students.age":7},{$set: {"students.$.school":101}},true,true)