MongoDB: creating an ObjectId for each new child added to the array field

mongodb 2.1.4 (Node driver)

I am currently trying to create a new ObjectID for every message that I insert into an array (the array is a subdocument).

As I understand it, all CRUD operations can be easily performed for each message in the array.

Example:

A collection of "threads" (Note-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"

        },
        ...
    ]
}

What I'm doing right now:

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

Problem:

Unlike insert for collections, updating with $ push does not create a new ObjectId for each message added to the array.

Question:

Is there a standard way to create an ObjectID during $ push into a child array? Or do we just manually create an ObjectID and add it to the child in advance?

+6
2

mongodb, , , , _id .

threads db, message messages, :

db.messages.insert({messages:[{_id:ObjectId(), message:"Message 1."}]}); 

_id:ObjectId(). :

enter image description here

, ObjectId(56...), . :

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

. :

enter image description here

, , :

enter image description here

_id messages .

_id . , Google , _id .

MongoDB 3.0.6 .

28-01-2016 16:09

MongoDB, , Node.js MongoDB. , ObjectID

var ObjectID = require('mongodb').ObjectID;

ObjectID , , update findOneAndUpdate

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);
    });
});

, . :

enter image description here

+7

, , .

, JSON ObjectID() , . _id, , , "56a970405ba22d16a8d9c30e" ObjectId("56a970405ba22d16a8d9c30e") . ObjectId 16 bytes.

-1

Source: https://habr.com/ru/post/1626276/


All Articles