更新时间:2022-10-27 18:37:13
你应该使用 Mongo 的 批量 API
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??
You should use Mongo's Bulk API