Use updatemany in Mongoose in a request

Asked

Viewed 97 times

1

I’m trying to make a update/Insert with the Mongoosis when finalizing a request. This request returns to me, for example:

let obj = [
    {nome: aaa,idade: 10},
    {nome: bbb,idade: 11},
    {nome: ccc,idade: 12},
]

And I’m trying to save to Mongo as follows: if any of these records do not exist, they should be created. If available, please update all document information.

The way I thought it would be the right one, reading the Mongo documentation, would be:

updateMany({}, {$set: {nome:"obj.$.nome", idade: "obj.$.idade"}}, {upsert:true})

But I must be doing something wrong, because just nothing happens, no data inserts, no error when using the catch.

Could someone help me ?

EDIT: Making a simpler test, by Mongoose I tried

model-do-contexto.updateMany({},{$set: {nome: 'abc'}},{upsert: true})

And it didn’t work. But going straight through the bank like this

db.contexo.updateMany({},{$set: {nome: 'abc'}},{upsert: true})

It worked, but I still don’t know how to make it work by Mongoose

  • you don’t need to take the db.seucontext.updateMany() to do this operation ? (I know nothing of it, only everywhere I saw, had something like that). Source 1 : https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#db.collection.updateMany Source 2: https://stackoverflow.com/questions/9038547/mongodb-update-every-document-on-one-field Source 3: https://mongoosejs.com/docs/transactions.html If what I said has nothing to do with it, just ignore kkk

  • I’m using Mongoose, it allows me to use the Model, that in the case, eh what you called context, then it would look something like this: exemplo.updateMany({...}). I did not put there, because I wanted to focus on the update function. =)

2 answers

1


I figured out what the problem was, in Mongoose, necessarily I need to pass the last parameter which is a function so to solve the initial problem the code would be:

for(let n of obj){
   <contexto>.update({nome: n.nome}, {$set: {'idade':n.idade}}, {upsert: true},  
(err, doc)=>{
    if(err) console.log(err);
    console.log(doc)
 })
}

That is, I had to go through all the objects that came in my request, and update them one by one by passing a function as a parameter in my update. I know this is the best way to solve it, but it worked. I didn’t see any of this in the documents I looked at.

0

Apparently you forgot to close the key that is open just before the $set:

{$set: {nome:"obj.$.nome", idade: "obj.$.idade"}

She has to stay like this:

{$set: {nome:"obj.$.nome", idade: "obj.$.idade"}}
  • I was wrong to type the question, but I had put.

  • Try the following code: model-do-contexto.update({},{$set: {nome: 'abc'}},{multi: true, upsert: true}) i read the documentation of Mongoose, and for multiple update the update() function is used and in the options of this function is indicated with the multi: true that multiple documents can be updated.

  • Using updateMany, there is no need to pass the {multi: true}. But I tested it anyway, and it didn’t work via code (Mongoose), but it worked straight through the bank ( like the other tests I did )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.