问题描述
假设我有一个像这样的 MongoDB 集合:
[{someId: 1, someProp: prop1, isOpen: true},{someId: 2, someProp: prop1, isOpen: true},{someId: 3, someProp: prop1, isOpen: true},{someId: 4, someProp: prop1, isOpen: true},]我想用一组对象调用它,但其中一些对象是新的,而另一些需要覆盖现有文档.
假设我的数组如下所示:
[{someId: 1, someProp: prop1, isOpen: true},{someId: 2, someProp: prop1, isOpen: true},{someId: 5, someProp: prop1, isOpen: true},{someId: 6, someProp: prop1, isOpen: true},]如何编写执行以下操作的 MongoDB 查询.
如果 someId
匹配我的第二个数组中的一个对象,只需将 isOpen
更改为 false
.如果 someId
不匹配任何对象,则将它们插入到集合中.
所以在我运行我的查询后,我会得到:
[{someId: 1, someProp: prop1, isOpen: false},{someId: 2, someProp: prop1, isOpen: false},{someId: 3, someProp: prop1, isOpen: true},{someId: 4, someProp: prop1, isOpen: true},{someId: 5, someProp: prop1, isOpen: true},{someId: 6, someProp: prop1, isOpen: true},]调用 forEach()
并使用:
db.books.update({项目:ZZZ135"},{项目:ZZZ135",库存: 5,标签: [ "数据库" ]},{ upsert: true })
...在循环中?可能不是.这意味着多次调用数据库.有没有更有效的方法来做到这一点?
基本上你需要 bulkWrite
操作与 upsert
const 数组 = [{ someId: 1, someProp: prop1, isOpen: true },{ someId: 2, someProp: prop1, isOpen: true },{ someId: 5, someProp: prop1, isOpen: true },{ someId: 6, someProp: prop1, isOpen: true }]模型.bulkWrite(array.map((数据) =>({更新一:{过滤器:{ someId: data.someId },更新:{ $set: { isOpen: false, someProp: data.someProp } },upsert:真的}}))})
会给你输出
[{ someId: 1, someProp: prop1, isOpen: false },{ someId: 2, someProp: prop1, isOpen: false },{ someId: 3, someProp: prop1, isOpen: true },{ someId: 4, someProp: prop1, isOpen: true },{ someId: 5, someProp: prop1, isOpen: true },{ someId: 6, someProp: prop1, isOpen: true }]Let's imagine that I have a MongoDB collection like so:
[
{someId: 1, someProp: prop1, isOpen: true},
{someId: 2, someProp: prop1, isOpen: true},
{someId: 3, someProp: prop1, isOpen: true},
{someId: 4, someProp: prop1, isOpen: true},
]
I want to make a call to it with an array of objects, but some of these objects will be new, and some will need to override existing documents.
So let's say my array looks like this:
[
{someId: 1, someProp: prop1, isOpen: true},
{someId: 2, someProp: prop1, isOpen: true},
{someId: 5, someProp: prop1, isOpen: true},
{someId: 6, someProp: prop1, isOpen: true},
]
How do I write a MongoDB query that does the following.
If someId
matches an object in my second array, just change the isOpen
to false
.If someId
doesn't match any objects then insert them into collection.
So after I run my query i would end up with:
[
{someId: 1, someProp: prop1, isOpen: false},
{someId: 2, someProp: prop1, isOpen: false},
{someId: 3, someProp: prop1, isOpen: true},
{someId: 4, someProp: prop1, isOpen: true},
{someId: 5, someProp: prop1, isOpen: true},
{someId: 6, someProp: prop1, isOpen: true},
]
Is it a good idea to call a forEach()
and just use:
db.books.update(
{ item: "ZZZ135" },
{
item: "ZZZ135",
stock: 5,
tags: [ "database" ]
},
{ upsert: true }
)
...in the loop? Probably not. That would mean multiple calls to the db. Is there a more efficient way of doing that?
Basically you need bulkWrite
operation with upsert
const array = [
{ someId: 1, someProp: prop1, isOpen: true },
{ someId: 2, someProp: prop1, isOpen: true },
{ someId: 5, someProp: prop1, isOpen: true },
{ someId: 6, someProp: prop1, isOpen: true }
]
Model.bulkWrite(
array.map((data) =>
({
updateOne: {
filter: { someId: data.someId },
update: { $set: { isOpen: false, someProp: data.someProp } },
upsert: true
}
})
)
})
Will give you the output
[
{ someId: 1, someProp: prop1, isOpen: false },
{ someId: 2, someProp: prop1, isOpen: false },
{ someId: 3, someProp: prop1, isOpen: true },
{ someId: 4, someProp: prop1, isOpen: true },
{ someId: 5, someProp: prop1, isOpen: true },
{ someId: 6, someProp: prop1, isOpen: true }
]
这篇关于一次调用即可创建和更新多个 MongoDB 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!