A
A
Alexey2019-11-21 00:02:19
MongoDB
Alexey, 2019-11-21 00:02:19

MongoDB how to update duplicates and insert new documents at the same time?

Every hour, an array of objects comes in, in which most of the duplicates and 20 percent of the new values.
The task is that, it is necessary to interpose all this array of documents into a database. Update duplicates, insert new values.
Used this approach:

postsModel.insertMany(result, (err, posts) => {
          if (err) {
            Logs.info(err);
          } else {
            Logs.info("Success!");
          }
        });

Exceptions... , adds the first (new) documents, nothing happens after that.
Q:
How do I update duplicates and insert new documents at the same time?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-11-21
@azovl

around this phrase, you need to google the solution, I think this answer may be suitable
https://stackoverflow.com/a/48451034
if these duplicate objects have some common ID or something that can be put in filter: { _id: unique_id },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question