简体   繁体   English

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

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

I have a mongoDB collection that contains JSON documents in the following format. 我有一个mongoDB集合,其中包含以下格式的JSON文档。 This is just a sample not the full document. 这只是一个示例,而不是完整的文档。

{
    "_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"
            },
      ]
      }
}

I have hundreds of similar documents in the collection. 我的收藏中有数百份类似的文件。 I want to write a query that will return the "artist" names that have a given set of tags and "count" values of those tags is greater than a given value. 我想编写一个查询,该查询将返回具有给定标签集的“艺术家”名称,并且这些标签的“计数”值大于给定值。

These are the two queries I have tried so far 这是我到目前为止尝试过的两个查询

  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 of the above queries work. 以上查询均无效。 I realize that the first one is fundamentally wrong because it does not take "count" value for the tags passed as a parameter. 我意识到第一个根本上是错误的,因为它不对作为参数传递的标签采用“计数”值。 But the second one I think should work. 但是我认为第二个应该起作用。 But I think my syntax is wrong. 但是我认为我的语法是错误的。 Where am I going wrong ? 我要去哪里错了?

From what I understand: 据我了解:

  • You have an array of tags to match; 您有一系列要匹配的标签;
  • You only take into account tags above a certain threshold. 您仅考虑超过特定阈值的标签。

As suggested by @yogesh in a comment, you should first ensure that your tag count is a number. 正如@yogesh在评论中建议的那样,您首先应该确保标签计数是数字。 Not as string. 不作为字符串。 Once done, you have to build your query based on your tag list. 完成后,您必须根据标签列表构建查询。 Something like that maybe: 可能是这样的

> 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
            }
        }
    }
]

Now, you can query your DB: 现在,您可以查询数据库:

> 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" } ] } }

Raise the threshold an do it all again, and you end-up selecting nothing: 提高阈值并再次执行所有操作,最终您什么也没有选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM