-1

i have array of objects and I want update all records in one query

var arrayObj=[
         {id:'5e346213bec252771415a9ee',
         status:1,
         date:01-2-2020},{id:'5e346213bec252471415a9efr',
         status:2,
         date:02-2-2020},
         {id:'5e346213bec252771415a9ee',
         status:3,
         date:01-3-2020}];

Leads.update();

I am new to node and mongo, how can I update this. I don't want to use loop, as I have done with loop. Now I want to learn this.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ali
  • 11
  • 8
  • i am waiting for reply...please reply friends – Ali Feb 01 '20 at 17:04
  • This question were solved here: https://stackoverflow.com/questions/54992810/update-many-in-mongoose – rpereira15 Feb 01 '20 at 17:28
  • thanks for your comment....please read my question.....your refread link dose not full fill my requirment – Ali Feb 01 '20 at 17:32
  • i have different values against each records... – Ali Feb 01 '20 at 17:33
  • @rpereira15 i want update all records with its own value – Ali Feb 01 '20 at 17:34
  • Actually, your need are not so explicit. You do not informed what do you want update. But, i really shure that link will be enough. You just need to execute a query without filter passing what filds who you want update. – rpereira15 Feb 01 '20 at 17:40
  • @rpereira15 var arrayObj=[ {id:'5e346213bec252771415a9ee', status:1, date:01-2-2020},{id:'5e346213bec252471415a9efr', status:2, date:02-2-2020}, {id:'5e346213bec252771415a9ee', status:3, date:01-3-2020}]; Leads.update(arrayOb); – Ali Feb 01 '20 at 17:48
  • is it right syntax to update all array records – Ali Feb 01 '20 at 17:49
  • these ids are exist in db and have other valuse...now i want to update new values – Ali Feb 01 '20 at 17:50
  • You can't do this. There's not support on mongo or mongoose for this. In some moment someone will do a loop (or you do or framework do). Look mongo doc: https://docs.mongodb.com/manual/reference/method/db.collection.update/#db.collection.update and mongoose doc: https://mongoosejs.com/docs/documents.html#updating – rpereira15 Feb 01 '20 at 17:53
  • respected @rpereira15 i didnt understand on this links description.i have told in my question i am begner in this field....can you make a proper example for me according to my question requirement..i will be very thankful of you. – Ali Feb 01 '20 at 18:27
  • i have stuck badly in this scenario...i have searched a lot but unable to find the solution – Ali Feb 01 '20 at 18:29

2 Answers2

1

What I trying to say, are:

  1. You can't bulk update in this way, just passing many objects to some magic function.
  2. MongoDb are a document oriented database, so, it's not normalised. To update some document, you need pass instructions (where you want to update and what you want to update).
  3. If you have an array, you need a loop function to update each array item.
  4. There's no possibility (without some third part library), to update many documents without loop. The links I sent explains the right way to do this.
halfer
  • 19,824
  • 17
  • 99
  • 186
rpereira15
  • 187
  • 7
0

You can use bulk operations.

var bulkOpr = <collectionName>.initializeUnorderedBulkOp();

bulkOpr.find({ _id: 1 }).updateOne({ /* update document */ });
bulkOpr.find({ _id: 2 }).updateOne({ /* update document */ });
// etc

bulkOp.execute();

You can do whatever you need to and the database do it all at once.

Ref. links:

https://docs.mongodb.com/manual/core/bulk-write-operations/

https://mongodb.github.io/node-mongodb-native/api-generated/unordered.html

Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28