S
S
Spawner2017-08-20 10:22:55
JavaScript
Spawner, 2017-08-20 10:22:55

How to add object from mongodb database?

In general, I have a script that calls db.get().collection('test').updateOne() passing in the ID I need and the object that I need to add to the "chatlog" key. I tried to implement it in different ways, but I could not make the object exactly ADDED, the .updateOne method just overwrites it. It looks like this:

{
  {
    "_id": 123123,
    "chatlog": {
      "971": {
        "msg": "message",
        "attachments": [
          
        ],
        "date": 1503212258
      }
    }
  }

After I execute the script passing the new "chatlog" with a different id, I just have the old one removed and replaced with the new one.
"972": {
  "msg": "message2",
  "attachments": [
  
  ],
  "date": 1503432654
}

There are thoughts on how to do this using node.js tools, that is, first get the base, then add a new chatlog to it using .push and send the result. But it seems to me that this is not done and there is a better way.
I just started learning mongodb, I only learned the basics, so don't shout at me too much)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
egor_nullptr, 2017-08-20
@egor_nullptr

https://docs.mongodb.com/manual/reference/operator...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question