如何最大程度地减少此MongoDB查询?
我整天都试图将所有这些查询都放在一个中,但是什么都没有用,请帮助 我需要更新计数,如果arset_id存在数组中,或插入{asset_id:id,count:1}如果不是 这是数组
{
from: '1',
to: '2',
json:[
{asset_id: id, count: 1},
{asset_id: id, count: 1},
{asset_id: id, count: 1}
]
}
,这是查询
{
updateOne: {
filter: {
from: res[i].from,
to: res[i].to,
},
update: {
from: res[i].from,
to: res[i].to,
},
upsert: true,
},
},
{
updateOne: {
filter: {
from: res[i].from,
to: res[i].to,
json: { $elemMatch: { asset_id: res[i].asset_id } },
},
update: {
$inc: { "json.$.count": 1 },
},
},
},
{
updateOne: {
filter: {
from: res[i].from,
to: res[i].to,
json: { $not: { $elemMatch: { asset_id: res[i].asset_id } } },
},
update: {
$push: {
json: { asset_id: res[i].asset_id, count: 1 },
},
},
//upsert: true,
},
}
whole day i'm trying to put all this query in one, but nothing work, pls help
I need to update count if asset_id exist in array, or insert {asset_id: id, count: 1} if not
here is array
{
from: '1',
to: '2',
json:[
{asset_id: id, count: 1},
{asset_id: id, count: 1},
{asset_id: id, count: 1}
]
}
and this is query
{
updateOne: {
filter: {
from: res[i].from,
to: res[i].to,
},
update: {
from: res[i].from,
to: res[i].to,
},
upsert: true,
},
},
{
updateOne: {
filter: {
from: res[i].from,
to: res[i].to,
json: { $elemMatch: { asset_id: res[i].asset_id } },
},
update: {
$inc: { "json.$.count": 1 },
},
},
},
{
updateOne: {
filter: {
from: res[i].from,
to: res[i].to,
json: { $not: { $elemMatch: { asset_id: res[i].asset_id } } },
},
update: {
$push: {
json: { asset_id: res[i].asset_id, count: 1 },
},
},
//upsert: true,
},
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果阵列的顺序不重要:您可以做:
查看其在
If the order of the array is not important you can do:
See how it works on the playground example