Mongoose update '不能使用部分 (..) 来遍历元素 [英] Mongoose update 'cannot use the part (..) to traverse the element
问题描述
我有一个非常烦人的问题,我无法使用猫鼬更新任何内容.使用起来真的很郁闷,文档也一点用都没有.
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.
我有这个架构:
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
},
devices : [{
id : String,
name : String
}]
});
这是我想将设备添加到数组 devices
的代码:
And this is the code where i want to add a device to the array 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");
}
});
}
此时我得到错误:errmsg: '不能使用部分(devices.id 的设备)来遍历元素 ({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": "$2a$10$7hXVHw7izcYlqbD6xe/te.0w2zucZ7lA007g9kXdoIMPhZhRyCIru",
"email": "a@a.a"
},
"__v": 0
}
推荐答案
使用 $push 或其他数组更新运算符向数组添加元素.详情请参考 http://docs.mongodb.org/手册/参考/操作员/更新/推送/#up._S_push
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
这篇关于Mongoose update '不能使用部分 (..) 来遍历元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!