P
P
partyzanx2020-05-30 03:19:49
MongoDB
partyzanx, 2020-05-30 03:19:49

How to update updateMany using each object's own properties?

I work with Mongoose

There is a collection with documents of this type
5ed1a61db7bd0250285362.png
Task: in each document from strokeNamesString to make strokeNamesArr using the operation

strokeNamesArr = strokeNamesString.split('\n').map(e => JSON.parse(e))

The code works if I save each one individually, but there are a lot of documents to save them manually, this code will not work
await Char.updateMany({},{strokeNamesArr: this.strokeNamesString.split('\n').map(e => JSON.parse(e))});

Because this.strokeNamesString is not retrieved

How do I extract this.strokeNamesString or is there another way to bulk write properties depending on other properties?
How do I refer to the current mutable document while running updateMany?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-05-30
@Robur

this.strokeNamesStringyou have nothing to do with the model, but with the function in which you call await Char.updateMany.
You cannot use a JS function in update that will be applied to each record, instead you can use a pipeline, and some expression for $set, if there is the functionality you need.
https://docs.mongodb.com/manual/reference/method/d...
If not, then update one by one.
or via bulkWrite - prepare all the data at once and send it in a batch in one operation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question