mongoDB:为添加到数组字段的每个新子项创建一个 ObjectId [英] mongoDB : Creating An ObjectId For Each New Child Added To The Array Field
问题描述
mongodb 2.1.4(节点驱动)
mongodb 2.1.4(The Node Driver)
我目前正在尝试为我插入到数组中的每条消息创建一个新的 ObjectID(该数组是一个子文档).
I'm currently trying to create a new ObjectID for each message I insert into an array(the array being a subdocument).
我是这样理解的 - 可以轻松地对数组中的每条消息执行所有 CRUD 操作.
I figure this way - All CRUD operations can easily be performed on each message in the array.
例如:
线程"集合(注意 - 每条消息的 ObjectId)
The "threads" collection(Note- An ObjectId for each message)
{
"_id": ObjectId("1234132413424123"), //A thread id
messages:[
{
_id :ObjectId("134124412341234"),// A message id
"message":"MongoDB is my friend"
},
{
_id :ObjectId("534124412342377"),
"message":"MongoDB is my friend too"
},
...
]
},
{
"_id": ObjectId("22341324134224234"),
messages:[
{
_id :ObjectId("8341244123411235"),
"message":"Something clever"
},
{
_id :ObjectId("134124412342376"),
"message":"blah blah blah"
},
...
]
}
我现在在做什么:
var query = {};
query["_id"] = new ObjectID(threadID);
var update = {$push: {}}; //I write the update object externally just for aesthetics
update.$push["messages"] = newMessage;
var threadsCollection = db.collection('threads');
threadsCollection.findOneAndUpdate(query,update, function (err, result) {
if (err) {
console.log(err);
}
db.close();
});
问题:
与集合的插入"不同,使用 $push 的更新不会创建添加到数组中的每条消息的新 ObjectId.
Unlike "insert" for collections, an update with $push does not create a new ObjectId for each message added to the array.
问题:
是否有在 $push into 期间创建 ObjectID 的标准方法子数组?还是我们应该手动创建一个 ObjectID 并事先将其添加到孩子?
Is there a standard way of creating an ObjectID during a $push into the child array? Or should we just manually create an ObjectID and add it to the child beforehand?
推荐答案
不是 mongodb
专家,但如果我理解正确,您希望自动插入子文档的 _id 字段.
Not a mongodb
expert but, if I understand you correctly, you wish the _id field of the subdocument to be inserted automatically.
我创建了 threads
db,然后使用以下命令将第一个 message
插入到 messages
集合中:
I created threads
db and then inserted the first message
in the messages
collection using the following command:
db.messages.insert({messages:[{_id:ObjectId(), message:"Message 1."}]});
注意 _id:ObjectId()
字段.结果如下:
Notice the _id:ObjectId()
field. The result is as follow:
现在我有了一个 ObjectId(56...)
,我可以更新该特定记录并向其中插入更多消息.命令如下:
Now that I have an ObjectId(56...)
, I can update that particular record and insert more messages to it. And the command is as follow:
db.messages.update({"_id":ObjectId("56...")},
{$push:{messages:{_id:ObjectId(), message:"I am message 2."}}});
以上将在集合中插入新消息.请参阅以下屏幕截图:
And the above would insert the new message in the collection. See below screenshots:
最后经过几次更新后,集合如下所示:
and finally after a few updates the collection looks as follow:
messages
数组中的所有 _id 字段都是自动生成的.
All the _id fields in the messages
array are automatically generated.
出于各种原因,使用 _id
字段可能不是一个好主意.有关是否使用 _id 作为子文档的键的更多详细信息,请谷歌.
It might not be a good idea to use _id
fields for various reasons. Please Google for more details on whether to use _id as key for subdocuments or not.
我使用 MongoDB shell 版本 3.0.6 作为命令.
编辑 28-01-2016 16:09
由于上述解决方案是基于 MongoDB shell,我决定使用 Node.js 驱动程序对 MongoDB 进行另一个测试.首先,我声明 ObjectID 变量如下
Since the above solution was based on MongoDB shell, I decided to do another test using Node.js driver for MongoDB. First of all, I declare ObjectID variable as follow
var ObjectID = require('mongodb').ObjectID;
我将在我的 update
和 findOneAndUpdate
函数调用中使用 ObjectID 和应该插入消息的线程的文档 ID
I will use the ObjectID with document id of the thread to which the message should be inserted as follow in my update
and findOneAndUpdate
function calls
app.get('/insertNewMessage', function(req, res) {
db.collection("messages").findOneAndUpdate({
_id: new ObjectID('56aa3554e90911b64c36a424')
}, {
$push: {
messages: {
_id: new ObjectID(),
message: "From NodeJS with <3 using findOneAndUpdate.Bye."
}
}
}, function(err, result) {
if (err)
res.json(err);
else
res.json(result);
});
});
两者都测试过,效果很好.请看下面的截图:
Tested both and works just fine. See the screenshot below:
这篇关于mongoDB:为添加到数组字段的每个新子项创建一个 ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!