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– Richard Willian
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. =)– Matheus Barbosa