R
R
Roman Andreevich2020-07-29 07:09:14
MongoDB
Roman Andreevich, 2020-07-29 07:09:14

How to do updateMany in Mongoose?

Colleagues, good time of the day, help, so to speak, who can do it) The essence of the problem is this, there is a collection in mongoDb and you need to update the documents in it, in fact, this is how the document looks

{
    "_id" : ObjectId("5f20eb9e69d73c62bcc03194"),
    "parseDate" : ISODate("2020-05-22T00:00:00.000Z"),
    "orderId" : 1,
    "order" : "SOME",
    "key" : 1.09683,
    "value" : 1.10082,
    "income" : 1.09375,
    "try" : 1.09477,
    "__v" : 0
}


and this is how an object looks like whose property methods to add:

{
    "parseDate": "2020-07-29T00:00:00.000Z",
     "order" : "SOME",
    "TRIP": 0.0061,
    "OKS": 93.33991119881563
  },


You need to update the data where order and parseDate match. I will be grateful for your help.

ps through the cycle everything is ok, the norms work. But how to do it without a loop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-07-29
@RomanDillerNsk

what exactly doesn't work?

db.collection.updateMany(
  { "parseDate": "2020-07-29T00:00:00.000Z", "order" : "SOME" },
  { $set: { "TRIP": 0.0061, "OKS": 93.33991119881563 } },
  { upsert: true }   // если ничего не найдет, то пусть сделает новый документ БД
)

^ should add fields to all appropriate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question