如何编写查询以根据 MongoDB 中的两个相关输入变量获取值?

How do I write a query to get values based on two dependent input variables in MongoDB?

我有一个 mongoDB collection,其中包含 JSON 以下格式的文档。这只是一个示例,并非完整文档。

{
    "_id": ObjectId("555ba8a6ae96b63b98969192"),
    "toptags": {
        "@attr": {
            "artist": "Rihanna"
        },
        "tag": [
            {
                "count": "100",
                "name": "pop",
                "url": "http://www.last.fm/tag/pop"
            },
            {
                "count": "89",
                "name": "rnb",
                "url": "http://www.last.fm/tag/rnb"
            },
            {
                "count": "60",
                "name": "female vocalists",
                "url": "http://www.last.fm/tag/female%20vocalists"
            },
            {
                "count": "55",
                "name": "dance",
                "url": "http://www.last.fm/tag/dance"
            },
            {
                "count": "40",
                "name": "Hip-Hop",
                "url": "http://www.last.fm/tag/hip-hop"
            },
            {
                "count": "21",
                "name": "Rihanna",
                "url": "http://www.last.fm/tag/rihanna"
            },
      ]
      }
}

我在 collection 中有数百个类似的文档。我想编写一个查询,该查询将 return 具有给定标签集的 "artist" 名称,并且这些标签的 "count" 值大于给定值。

这是我目前尝试过的两个查询

  1. collection_name.find({'$and': [{"toptags.tag.name":tag_array},
                                   {"toptags.tag.count":{'$gte':count_value}}]},
                         {"_id":"1","toptags.@attr.artist":"1"})
    
  2. collection_name.find({"toptags.artist":
                            {$all : [{"$elemMatch" : 
                                        {"name":tag_array, 
                                         "count": {'$gt': count_value}}},]})
    

None 以上查询有效。我意识到第一个从根本上是错误的,因为它没有为作为参数传递的标签取 "count" 值。 但是我认为第二个应该可行。但我认为我的语法是错误的。 我哪里错了?

据我了解:

  • 您有一组要匹配的标签;
  • 您只考虑超过特定阈值的标签。

正如@yogesh 在评论中所建议的,您应该首先确保您的标签计数是一个数字。不是字符串。完成后,您必须根据标签列表构建查询。 喜欢的东西可能:

> THRESHOLD=50
> TAGS=['dance', 'rnb']
> for (idx in TAGS) {
    QTAGS[idx]={"$elemMatch": {"name":TAGS[idx], "count":{"$gt": THRESHOLD}}}
  }
> QTAGS
[
    {
        "$elemMatch" : {
            "name" : "dance",
            "count" : {
                "$gt" : 50
            }
        }
    },
    {
        "$elemMatch" : {
            "name" : "rnb",
            "count" : {
                "$gt" : 50
            }
        }
    }
]

现在,您可以查询您的数据库:

> db.w.find({"toptags.tag": { "$all": QTAGS}})
{ "_id" : ObjectId("555ba8a6ae96b63b98969192"), "toptags" : { "@attr" : { "artist" : "Rihanna" }, "tag" : [ { "count" : 100, "name" : "pop", "url" : "http://www.last.fm/tag/pop" }, { "count" : 89, "name" : "rnb", "url" : "http://www.last.fm/tag/rnb" }, { "count" : 60, "name" : "female vocalists", "url" : "http://www.last.fm/tag/female%20vocalists" }, { "count" : 55, "name" : "dance", "url" : "http://www.last.fm/tag/dance" }, { "count" : 40, "name" : "Hip-Hop", "url" : "http://www.last.fm/tag/hip-hop" }, { "count" : 21, "name" : "Rihanna", "url" : "http://www.last.fm/tag/rihanna" } ] } }

提高门槛并重新做一遍,最后什么都没有选择:

> THRESHOLD=100
> for (idx in TAGS) {   QTAGS[idx]={"$elemMatch": {"name":TAGS[idx], "count":{"$gt": THRESHOLD}}} }
> db.w.find({"toptags.tag": { "$all": QTAGS}})
> // nothing