我有一个非常恼人的问题,我不能使用mongoose更新任何东西。它的使用真的很令人沮丧,而且文档也没有任何帮助。
我有这样的模式:
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
},
devices : [{
id : String,
name : String
}]
});
下面是我想要向数组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: 'cannot use the part (devices of devices.id) to traverse the element ({devices: []})' }
我没能找到一个解释为什么会发生这种情况。我不得不提一下这个文档(数据库中几乎只有一个文档),是这样的:
{
"_id": {
"$oid": "5585a196fe11b21100635c74"
},
"devices": [],
"local": {
"password": "$2a$10$7hXVHw7izcYlqbD6xe/te.0w2zucZ7lA007g9kXdoIMPhZhRyCIru",
"email": "a@a.a"
},
"__v": 0
}
发布于 2015-06-21 01:47:24
使用$push或其他数组更新运算符向数组添加元素。有关详情,请参阅http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push
https://stackoverflow.com/questions/30956959
复制相似问题