简体   繁体   中英

MongoDB updateMany, dynamic filter field

When using the .updateMany(filter, update, option). How can I set a dynamic filter field for every document?

I have an array of documents like this:

[
    {
        time: 1,
        data: []
    },
    {
        time: 2,
        data: []
    },
    {
        time: 3,
        data: []
    }
]

Each with a unique timestamp. In case the timestamp already exists, the corresponding document should be updated with the new data but if the time doesn't exists, create a new document.

https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/

But as it seems, .updateMany() only allows a static filter field. Like this

Candles.updateMany({time: 'someStaticTime'}, documents);

while I would prefer something like this

Candles.updateMany({time: $currentInsertingDocument.time}, documents);

Is this possible without looping over each document seperate??

您应该使用Mongo的Bulk API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM