我试着在全球范围内修正一些数据..。为什么我要得到这个error...TypeError:对象函数模型(doc,field,skipId) { if (!(这个模型的实例))r
我得到所有的数据,它应该更新,但它不是。而且误差很大。
UpdateAllThings: function(req, res) {
things.find().exec( function(err, items) {
items.forEach(function(th) {
th.Features.forEach(function(f) {
thingsfeatures.findOne({'fName': f.fName},function(err, item) {
if (item != null ) {
try{
things.Update({Features: {"$elemMatch":{_id:f._id}}}, {$addToSet: {'Features.$.tType': item.tType, 'Features.$.fGroup': item.fGroup }}, function (err, inventory) {
if (err) return handleError(err);
});
}catch(err){
if (err) return handleError(err);
}
}
});
});
});
});
return res.send('thanks please come again');
}*_________________________*
当然:这或多或少是.
{
"Features" : [{
"_id" : ObjectId("5013c9742df16337609b2706"),
"tID" : "5013c9742df16337609b26f4",
"tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
"fName" : "Retail Price:",
"fValue" : "899.99",
"_keywords " : ["899.99"]
}, {
"_id" : ObjectId("5013c9742df16337609b2707"),
"_keywords " : ["1", "x", "Proprietary", "LP-E8", "Lithium-ion", "rechargeble"],
"fGroup" : ["Power"],
"fName" : "Batteries Included:",
"fValue" : "1 x Proprietary LP-E8 Lithium-ion rechargeble",
"tID" : "5013c9742df16337609b26f4",
"tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
"tType" : ["Cammera"]
}],
"_id" : ObjectId("5013c9742df16337609b26f4"),
"tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
"tType" : "Camera"
}这是可行的,但是它添加了一个包含数据的数组,而不仅仅是我想要的新字段和数据。
db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $addToSet : { "Features.$.fType" : "Cammera", "Features.$.fGroup" : "Power" }})发布于 2012-08-30 18:45:16
发行一个大写。谢谢约翰尼
第二个问题: Addtoset创建数组,而不是将数组添加到它创建新集合的集合中。所以,只需使用set。(令人困惑的文件)
第三,要更新嵌套的set,只需这样做:这比使用元素匹配和其他表单要好得多。
db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $set : { "Features.$.fType" : "Cammera", "Features.$.fGroup" : "Power" }})https://stackoverflow.com/questions/12183498
复制相似问题