0

It is possible to insert an array of objects, but not possible to update them. You have to remove them and then insert them. I don't want to have to remove and then insert them, because if the remove goes well but then the insert has an error, the data is lost. How can I update an array (all documents) in a collection, properly?

Inserting array of documents:

collection.insert(arrToInsert, function(err, results){
    console.log(results.ops);
});

Updating inserted array (not good):

collection.remove({}, function(err){
    collection.insert(arrUpdated, function(err, result) {
        console.log(results.ops);
    });
});

I have tried using collection.update but this does not allow an array.

edit: For example: Insert this array:

[
    {_id:0,name:"Harry"},
    {_id:1,name:"Jacob"},
    {_id:2,name:"Bob"}
]

and then, change it to this:

[
    {_id:0,name:"Dennis"},
    {_id:1,name:"Peter"},
    {_id:2,name:"Ghandi"}
]

My actual case is a bit more complicated, because the array has another key/value pair, and can be changed in many ways.

2
  • Really hard to tell what you are doing because your question is not phrased with a clear example. You perhaps mean multiple .update() operations with .bulkWrite(). It's generally better to "show us" rather than talk about something you don't actually show, which is what your question presently does. See How to create a Minimal, Complete, and Verifiable example in the help center which gives you tips on how you structure a question so people understand what you mean and how to solve it. Commented Oct 17, 2017 at 21:45
  • but with .bulkWrite each object in the array must have a key e.g. {insertOne : { "document" :_id:0,name:"Dennis"} ... which makes no sense and I cannot iterate each object in the array with bulkWrite Commented Oct 17, 2017 at 22:20

1 Answer 1

1

You mean .bulkWrite(), which allows multiple update operations ( Insert/Update/Remove) on the same collection in a single batch request. It actually is what modern drivers really call when either .insert() or .insertMany() is called with an array of documents.

Your new update would be:

var arrToUpdate = [
    {_id:0,name:"Dennis"},
    {_id:1,name:"Peter"},
    {_id:2,name:"Ghandi"}
];

collection.bulkWrite(
  arrToUpdate.map( d => ({
    "replaceOne": {
      "filter": { "_id": d._id },
      "replacement": d
    }
  })),
  function(err,result) {
    console.log(result)
  }
)

Where replaceOne is essentially an "update" without using any modifiers such as $set.

The point is that just like .insert() with an array or .insertMany(), the database receives only one request and response, but there are multiple operations in that request.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks, that looks right. although it has a syntax error, bad brackets?
@GeorgeCampbell Yes, missing a bracket. That's what I get for typing directly into the answer.
I actually had to use collection.find({}) to get the document because the result didn't return it.
@GeorgeCampbell It is not meant to return it. The only reason insertMany() does is because you actually supplied the document to insert, so it just maps the assigned _id values ( if none was supplied ) onto the documents you provide. No form up "update" aside from the .findAndModify() variants "return the document". For which I might add, since you are supplying the documents then what possible use is a .find()? You already know what you sent.
next problem... if I change the array to an empty array [] it has an error."Invalid Operation, no operations specified"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.