【发布时间】:2016-09-13 10:27:44
【问题描述】:
我正在使用 mongodb native 和 Node.js 6.5.0。
我在 mongodb 中有用户对象,其结构如下:
{
"_id" : ObjectId("57d7d294d96a73d128c46db9"),
"id" : "105862592064",
"labels" : [
]
}
我有一个循环(对于找到的每个用户)从 API 获取数据,然后将其推送到数组类型的对象属性中。其中用户 ID user.id 和要推送的数据是 resp.labels。
这是我的代码:
db.collection('users').update(
{"id":user.id},
{"$push":{"users.labels":resp.labels}}
)
它不返回任何错误,也不更新对象。我做错了什么?
【问题讨论】:
-
db.collection('users').update( { _id: user.id, labels: [] }, { $set: { "labels.$" : resp.labels } } ) 试试有了这个
标签: javascript node.js mongodb node-mongodb-native