使用聚集

发布于 2025-02-09 16:22:02 字数 433 浏览 1 评论 0 原文

我正在尝试在Mongo文档中搜索一个内部数组中的关键字。

{
  "PRODUCT_NAME" : "Truffle Cake",

  "TAGS": [
            ["Cakes", 100],
            ["Flowers", 100],
       ]
}

通常,我会做这样的事情,它会起作用。

db.collection.find( {"TAGS":{"$elemMatch":{ "$elemMatch": {"$in":['search_text']} } }} )

但是现在,由于其他要求,我将此查询更改为基于汇总的查询。我已经尝试过$ filter,$匹配,但无法准确复制上述查询。.

任何人都可以转换上述代码以便可以直接与汇总合作吗? (我使用pymongo)

I am trying to search for a keyword inside array of arrays in a Mongo document.

{
  "PRODUCT_NAME" : "Truffle Cake",

  "TAGS": [
            ["Cakes", 100],
            ["Flowers", 100],
       ]
}

Usually, I would do something like this and it would work.

db.collection.find( {"TAGS":{"$elemMatch":{ "$elemMatch": {"$in":['search_text']} } }} )

But now, I changed this query to an aggregate based query due to other requirements. I've tried $filter , $match but not able to replicate the above query exactly..

Can anyone convert the above code so that it can directly work with aggregate?
(I use PyMongo)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

咆哮 2025-02-16 16:22:02

$ matt (查找),来自文档:

查询语法与读取操作查询语法相同;

这意味着,如果您有一个在“查找”中起作用的查询,它也将在 $ match 阶段中使用,例如:

db.collection.aggregate([
  {
    $match: {
      "TAGS": {
        "$elemMatch": {
          "$elemMatch": {
            "$in": [
              "Cakes"
            ]
          }
        }
      }
    }
  }
])

mongo playground

$match uses the same query syntax as the query language (find), from the docs:

The query syntax is identical to the read operation query syntax;

This means if you have a query that works in a "find", it will also work within a $match stage, like so:

db.collection.aggregate([
  {
    $match: {
      "TAGS": {
        "$elemMatch": {
          "$elemMatch": {
            "$in": [
              "Cakes"
            ]
          }
        }
      }
    }
  }
])

Check this live on Mongo Playground

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