mongodb在数组中找到字段

发布于 2025-01-23 17:58:44 字数 1973 浏览 0 评论 0原文

我正在尝试学习mongoDB,我的数据库看起来像这样:

    {
        "_id" : ObjectId("625c95836be34e3c73c37316"),
        "cuisine" : "Chinese",
        "grades" : [
            {
                "date" : ISODate("2014-02-25T00:00:00Z"),
                "grade" : "A",
                "score" : 112
            },
            {
                "date" : ISODate("2013-08-14T00:00:00Z"),
                "grade" : "C",
                "score" : 28
            },
            {
                "date" : ISODate("2012-07-18T00:00:00Z"),
                "grade" : "A",
                "score" : 13
            },
            {
                "date" : ISODate("2012-03-09T00:00:00Z"),
                "grade" : "A",
                "score" : 13
            },
            {
                "date" : ISODate("2011-10-27T00:00:00Z"),
                "grade" : "A",
                "score" : 12
            },
            {
                "date" : ISODate("2011-05-19T00:00:00Z"),
                "grade" : "A",
                "score" : 13
            }
        ],
        "name" : "janette"
    },
    {
        "_id" : ObjectId("625c95836be34e3c73c37317"),
        "cuisine" : "Chinese",
        "grades" : [
            {
                "date" : ISODate("2014-01-07T00:00:00Z"),
                "grade" : "A",
                "score" : 17
            },
            {
                "date" : ISODate("2013-05-09T00:00:00Z"),
                "grade" : "B",
                "score" : 107
            }
        ],
        "name" : "Annie'S Kitchen"
    }

每个对象都有一个拥有相同字段的数组。如果我尝试:

db.restaurants.find({"grades.score":{ $gt: 80, $lt: 100 }});

它会抓住两者,但不会抓住。 我还希望能够检查:

db.restaurants.find("grades.score":{$gt: 70},"grades.grade":{/^A$/} });

我需要仅在数组上的字段中检查字段。

我不知道如何更好地解释它,对不起,并感谢您的帮助。

I'm trying to learn mongodb, my database looks like this:

    {
        "_id" : ObjectId("625c95836be34e3c73c37316"),
        "cuisine" : "Chinese",
        "grades" : [
            {
                "date" : ISODate("2014-02-25T00:00:00Z"),
                "grade" : "A",
                "score" : 112
            },
            {
                "date" : ISODate("2013-08-14T00:00:00Z"),
                "grade" : "C",
                "score" : 28
            },
            {
                "date" : ISODate("2012-07-18T00:00:00Z"),
                "grade" : "A",
                "score" : 13
            },
            {
                "date" : ISODate("2012-03-09T00:00:00Z"),
                "grade" : "A",
                "score" : 13
            },
            {
                "date" : ISODate("2011-10-27T00:00:00Z"),
                "grade" : "A",
                "score" : 12
            },
            {
                "date" : ISODate("2011-05-19T00:00:00Z"),
                "grade" : "A",
                "score" : 13
            }
        ],
        "name" : "janette"
    },
    {
        "_id" : ObjectId("625c95836be34e3c73c37317"),
        "cuisine" : "Chinese",
        "grades" : [
            {
                "date" : ISODate("2014-01-07T00:00:00Z"),
                "grade" : "A",
                "score" : 17
            },
            {
                "date" : ISODate("2013-05-09T00:00:00Z"),
                "grade" : "B",
                "score" : 107
            }
        ],
        "name" : "Annie'S Kitchen"
    }

each object has an array that owns the same fields. If I try:

db.restaurants.find({"grades.score":{ $gt: 80, $lt: 100 }});

it will catch both but should catch none.
Also I want to be able to check:

db.restaurants.find("grades.score":{$gt: 70},"grades.grade":{/^A$/} });

I need to check the fields only with the fields in the same position on the array.

I don't know how to explain it better, sorry, and thanks for the help.

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

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

发布评论

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

评论(1

自控 2025-01-30 17:58:44

您可以使用$ MAP来投影辅助布尔数组,并使用$ allelementTrue$ anyelementTrue将其过滤。

db.restaurants.aggregate([
  {
    "$match": {
      $expr: {
        "$allElementsTrue": {
          "$map": {
            "input": "$grades",
            "as": "g",
            "in": {
              $and: [
                {
                  $gt: [
                    "$g.score",
                    80
                  ]
                },
                {
                  $lt: [
                    "$g.score",
                    100
                  ]
                }
              ]
            }
          }
        }
      }
    }
  }
])

这是您的 first 第二查询

You can use $map to project an auxiliary boolean array and use $allElementsTrue or $anyElementTrue to filter it.

db.restaurants.aggregate([
  {
    "$match": {
      $expr: {
        "$allElementsTrue": {
          "$map": {
            "input": "$grades",
            "as": "g",
            "in": {
              $and: [
                {
                  $gt: [
                    "$g.score",
                    80
                  ]
                },
                {
                  $lt: [
                    "$g.score",
                    100
                  ]
                }
              ]
            }
          }
        }
      }
    }
  }
])

Here are the Mongo playgrounds for your first and second query.

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