如何最大程度地减少此MongoDB查询?

发布于 2025-02-12 18:26:38 字数 1204 浏览 0 评论 0原文

我整天都试图将所有这些查询都放在一个中,但是什么都没有用,请帮助 我需要更新计数,如果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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2025-02-19 18:26:38

如果阵列的顺序不重要:您可以做:

db.collection.aggregate([
  {$set: {
      jsonWithOut: {
        $filter: {
          input: "$json", cond: {$ne: ["$this.asset_id", dataToInsert.asset_id]}}
      },
      jsonOfNew: {
        $filter: {
          input: "$json", cond: {$eq: ["$this.asset_id", dataToInsert.asset_id]}}
      },
      json: "$REMOVE"
    }
  },
  {$set: {
      jsonOfNew: {
        $cond: [
          {$eq: [{$size: "$jsonOfNew"}, 0]},
          [dataToInsert],
          [
            {
              asset_id: "$dataToInsert.asset_id",
              count: {$add: [{$first: "$jsonOfNew.count"}, 1]}
            }
          ]
        ]
      },
      dataToInsert: "$REMOVE"
    }
  },
  {$set: {
      json: {$concatArrays: ["$jsonWithOut", "$jsonOfNew"]},
      jsonOfNew: "$REMOVE",
      jsonWithOut: "$REMOVE"
    }
  }
])

查看其在

If the order of the array is not important you can do:

db.collection.aggregate([
  {$set: {
      jsonWithOut: {
        $filter: {
          input: "$json", cond: {$ne: ["$this.asset_id", dataToInsert.asset_id]}}
      },
      jsonOfNew: {
        $filter: {
          input: "$json", cond: {$eq: ["$this.asset_id", dataToInsert.asset_id]}}
      },
      json: "$REMOVE"
    }
  },
  {$set: {
      jsonOfNew: {
        $cond: [
          {$eq: [{$size: "$jsonOfNew"}, 0]},
          [dataToInsert],
          [
            {
              asset_id: "$dataToInsert.asset_id",
              count: {$add: [{$first: "$jsonOfNew.count"}, 1]}
            }
          ]
        ]
      },
      dataToInsert: "$REMOVE"
    }
  },
  {$set: {
      json: {$concatArrays: ["$jsonWithOut", "$jsonOfNew"]},
      jsonOfNew: "$REMOVE",
      jsonWithOut: "$REMOVE"
    }
  }
])

See how it works on the playground example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文