【发布时间】:2018-05-22 21:29:12
【问题描述】:
我的数据库中有这种格式
[{
"birth-date": "18/12/2010",
"babies": [{
"id":1,
"name": "James",
"age": 8,
}, {
"id":2,
"name": "John",
"age": 4,
}]
}]
所以我想将 James 的名字更新为其他名称。我愿意
Babies.update({"birth-date":date, 'babies.id': 1}, {'$set': {'babies.$.name':"something else"}}, function(err, response){
res.json(response);
})
它返回 ok:0 第二个选择器 ('babies.id': 1) 有问题。
【问题讨论】:
-
我不知道 mongoose,但是在 mongo shell 中
Babies.update({"birth-date", 'babies.id': 1},...会报错因为"birth-date"需要:某事,即该属性需要一个密钥(例如Babies.update({"birth-date":"18/12/2010", 'babies.id': 1},... -
@RafaelCaballero 更新了我的答案,只是错字。
-
你可以尝试用 '$set' 替换 set 部分:{'babies.$.name':"something else"} 吗?
-
@SagarReddy 试过了,不行。
-
你是否错过了 set 的结束卷曲?
标签: node.js mongodb express mongoose