问题描述
当使用 .updateMany(filter, update, option) 时.如何为每个文档设置动态过滤器字段?
我有一个这样的文档数组:
[{时间:1,数据: []},{时间:2,数据: []},{时间:3,数据: []}]每个都有唯一的时间戳.如果时间戳已存在,则应使用新数据更新相应的文档,但如果时间不存在,则创建一个新文档.
https://docs.mongodb.com/manual/参考/方法/db.collection.updateMany/
但看起来,.updateMany() 只允许静态过滤器字段.像这样
Candles.updateMany({time: 'someStaticTime'}, 文件);
虽然我更喜欢这样的东西
Candles.updateMany({time: $currentInsertingDocument.time}, 文档);
这是否可以不单独循环遍历每个文档??
你应该使用 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
这篇关于MongoDB updateMany,动态过滤字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!