M
M
Makcimka2018-12-30 18:22:30
MongoDB
Makcimka, 2018-12-30 18:22:30

Is there an update query in mongo db such that if there is a record in the database, then update it, if it does not exist, then create a new record in the collection?

The bottom line is, I add data in a cycle

for(var i = 0; i<arrAll.length; i++){
          db.get().collection('bookEntries').insert({
              id: arrAll[i].id,
              author: arrAll[i].author,
              authorLink: arrAll[i].authorLink,
              authorThumb: arrAll[i].authorThumb,
        })
    }

accordingly, in the collection it looks something like this
{
_id: ObjectId("5c1ce16371325e0f404952c9"),
id: 1234897678634,
author: 'some kind of author's record'
authorLink: 'something else'
authorThumb: 'there seems to be a link here'
}
mongodb driver "^3.1 .10"
Instead of "insert" there was "update" now it is outdated, which checked by "id" and updated it all, if it did not find it created a new one, I'm wondering if there is such an "update" now in the new version of mongi or is it worth checking for the existence of records in the collection?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Tiunov, 2018-12-30
@Dizast

https://docs.mongodb.com/manual/reference/method/d...
The update method with the upsert=True argument updates an existing document or creates a new one if it didn't exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question