admin管理员组文章数量:1323381
I am accessing MongoDB documents with a node.js web-front-end while in developement.
I got now following document:
{
"_id": ObjectID("500abe6a25dff13c7c000001")
, "username": "kyogron"
, "email": "[email protected]"
, "contacts": [ObjectID("500abe6a2543213c7c000002")] // this should contain other user's id
}
I now want to add another user_id to the contacts array manually, before implementing the feature in the front-end.
As you see above I alread tried this using the ObjectID keyword but this didn't work...
I am accessing MongoDB documents with a node.js web-front-end while in developement.
I got now following document:
{
"_id": ObjectID("500abe6a25dff13c7c000001")
, "username": "kyogron"
, "email": "[email protected]"
, "contacts": [ObjectID("500abe6a2543213c7c000002")] // this should contain other user's id
}
I now want to add another user_id to the contacts array manually, before implementing the feature in the front-end.
As you see above I alread tried this using the ObjectID keyword but this didn't work...
Share Improve this question edited Jul 22, 2012 at 16:13 Asya Kamsky 42.4k5 gold badges112 silver badges133 bronze badges asked Jul 22, 2012 at 15:17 bodokaiserbodokaiser 15.8k27 gold badges100 silver badges143 bronze badges2 Answers
Reset to default 5This worked for me from the MongoDB shell, I'm using $addToSet
instead of $push
because I assume you want to avoid dupes:
var o = new ObjectId();
db.foo.update({}, {$addToSet : {"contacts" : o}});
var o = new ObjectId();
db.foo.update({}, {$addToSet : {"contacts" : o}});
That gave me a document that looks like this (my foo collection only contained your sample, so I didn't have to have a specific matching criteria):
{
"_id" : ObjectId("500c2118c78bb07bfbb69eb3"),
"contacts" : [
ObjectId("500c20efc78bb07bfbb69eb2"),
ObjectId("500c227ac78bb07bfbb69eb6")
],
"email" : "[email protected]",
"username" : "kyogron"
}
In mongo shell run the following update:
db.collection.update({"_id" : ObjectId("500abe6a25dff13c7c000001")},
{$push:{"contacts": ObjectId("500abe6a2543213c7c000002")}})
Use appropriate ObjectId values for your specific case.
本文标签: javascriptHow do I manually add a reference in MongoDBStack Overflow
版权声明:本文标题:javascript - How do I manually add a reference in MongoDB? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121072a2421699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论