mongoDB:为添加到数组字段的每个新子级创建ObjectId

mongoDB:为添加到数组字段的每个新子级创建ObjectId,mongodb,Mongodb,mongodb 2.1.4(节点驱动程序) 我目前正试图为插入数组(数组是一个子文档)的每条消息创建一个新的ObjectID 我这样想——所有CRUD操作都可以轻松地对数组中的每条消息执行 例如: var query = {}; query["_id"] = new ObjectID(threadID); var update = {$push: {}}; //I write the update object externally just for aesthetics updat

mongodb 2.1.4(节点驱动程序)

我目前正试图为插入数组(数组是一个子文档)的每条消息创建一个新的ObjectID

我这样想——所有CRUD操作都可以轻松地对数组中的每条消息执行

例如:

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();
});
“线程”集合(注意-每条消息的ObjectId)

我现在正在做的事情:

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();
});
问题:

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

问题:

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-into过程中是否有创建ObjectID的标准方法
子数组?或者我们应该手动创建一个ObjectID,并事先将其添加到子对象中吗

不是
mongodb
专家,但如果我理解正确,您希望自动插入子文档的_id字段

我创建了
线程
db,然后使用以下命令将第一条
消息
插入
消息
集合中:

db.messages.insert({messages:[{_id:ObjectId(), message:"Message 1."}]}); 
请注意
\u id:ObjectId()
字段。结果如下:

db.messages.update({"_id":ObjectId("56...")}, 
{$push:{messages:{_id:ObjectId(), message:"I am message 2."}}});

现在我有了一个
ObjectId(56…
),我可以更新那个特定的记录并向其中插入更多的消息。命令如下:

db.messages.update({"_id":ObjectId("56...")}, 
{$push:{messages:{_id:ObjectId(), message:"I am message 2."}}});
以上内容将在集合中插入新消息。请参见以下屏幕截图:

最后,经过几次更新后,集合看起来如下所示:

db.messages.update({"_id":ObjectId("56...")}, 
{$push:{messages:{_id:ObjectId(), message:"I am message 2."}}});

消息
数组中的所有_id字段都会自动生成

出于各种原因,使用
\u id
字段可能不是一个好主意。有关是否使用_id作为子文档密钥的更多详细信息,请使用谷歌

我对命令使用了MongoDB shell版本3.0.6

编辑28-01-2016 16:09

由于上述解决方案基于MongoDB shell,因此我决定使用Node.js驱动程序对MongoDB进行另一项测试。首先,我声明ObjectID变量如下

var ObjectID = require('mongodb').ObjectID;
在我的
update
findOneAndUpdate
函数调用中,我将使用ObjectID和消息应该插入到的线程的文档id,如下所示

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);
    });
});
app.get('/insertNewMessage',函数(req,res){
db.collection(“messages”).findOneAndUpdate({
_id:新对象id('56aa3554e90911b64c36a424')
}, {
$push:{
信息:{
_id:new ObjectID(),

消息:“来自NodeJS with很抱歉协议的漏洞,但我没有足够的代表对主线程发表评论

我一直在研究如何使用
ObjectID()
生成
JSON
插入,在我的旅行中发现这个解决方案不太适合索引。建议的解决方案具有字符串id值,而不是真正的对象id-即。
“56a970405ba22d16a8d9c30e”不同于
ObjectId(“56a970405ba22d16a8d9c30e”)
和使用该字符串进行索引将更慢。
ObjectId
实际上在内部表示为16个
字节

如果您使用节点驱动程序,这里对该解决方案进行了一点修改。您可以像下面这样附加到将添加到数组中的子对象
newMessage[“\u id”]=newobjectid();
然后像这样更新集合
threadCollection.findOneAndUpdate(查询,{$push:{messages:newMessage}}})
我在谷歌上搜索过这样做是否是一种好的做法,但没有给出确切的答案。至于这是否是一种好的做法,这可能是一个很好的阅读方法,这是一种更好的方法。您也可以使用sequence generator来生成子对象