node.js Mongoose update '不能使用部分 (..) 来遍历元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30956959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose update 'cannot use the part (..) to traverse the element
提问by Edeph
I have this really annoying issue where i can't update anything using mongoose. It's really frustrating to use, and the documentation is not helping at all.
我有一个非常烦人的问题,我无法使用猫鼬更新任何内容。使用起来真的很令人沮丧,而且文档根本没有帮助。
I have this schema:
我有这个架构:
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
},
devices : [{
id : String,
name : String
}]
});
And this is the code where i want to add a device to the array devices:
这是我想向数组添加设备的代码devices:
function updateDeviceList(user, deviceID, deviceName)
{
User.update({ 'local.email' : user},
{ $set: {'devices.id' : deviceID, 'devices.name' : deviceName}},
function(err, response)
{
if(err)
{
console.log("Update device error", err);
}
else {
console.log("Update device OK");
}
});
}
At this point i get the error:
errmsg: 'cannot use the part (devices of devices.id) to traverse the element ({devices: []})' }
此时我收到错误:
errmsg: 'cannot use the part (devices of devices.id) to traverse the element ({devices: []})' }
I didn't manage to find an explanation to why this is happening. I have to mention that the document (there is pretty much only one document in the database), is this one:
我没有设法解释为什么会发生这种情况。我不得不提到这个文件(数据库中几乎只有一个文件),是这个:
{
"_id": {
"$oid": "5585a196fe11b21100635c74"
},
"devices": [],
"local": {
"password": "ahXVHw7izcYlqbD6xe/te.0w2zucZ7lA007g9kXdoIMPhZhRyCIru",
"email": "[email protected]"
},
"__v": 0
}
采纳答案by Sasikanth Bharadwaj
Use the $push or other array update operators to add elements to an array. For details, refer http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push
使用 $push 或其他数组更新运算符向数组添加元素。详情请参考http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push
回答by chridam
Try to use the positional $operatorin the update which identifies an element in an array to update without explicitly specifying the position of the element in the array, but this will only ever match one element at a time:
尝试在更新中使用位置$运算符来标识要更新的数组中的元素,而无需明确指定该元素在数组中的位置,但这一次只会匹配一个元素:
User.update(
{
"local.email": user,
"devices.id": { "$ne": deviceID },
"devices.name": { "$ne": deviceName }
},
{
"$set": {
"devices.$.id": deviceID,
"devices.$.name": deviceName
}
}
);
From the docs, the positional $ operator acts as a placeholder for the first element that matches the query document, and the array field must appear as part of the query document hence the query document
从文档中,位置 $ 运算符充当匹配查询文档的第一个元素的占位符,并且数组字段必须作为查询文档的一部分出现,因此查询文档
"devices.id": { "$ne": deviceID },
"devices.name": { "$ne": deviceName }
contains the devicearray and will match those documents where the devicearray idis not equal to deviceIDand the name is not the same as the name which you are trying to update. This will even match documents where the devicearray is empty.
包含device数组并将匹配那些device数组id不等于deviceID且名称与您尝试更新的名称不同的文档。这甚至会匹配device数组为空的文档。

