在MongoDB中使用特定的HashTag查找对象[tweets]

时间:2019-01-18 02:04:50

标签: mongodb mongodb-query

我想获取具有特定标签(JoinPositivePakistan)的所有对象[tweets]

这是MongoDB中对象的示例

{
"_id" : ObjectId("5c3d8522950e2018ccfe94da"),
"created_at" : "Tue Jan 15 07:00:29 +0000 2019",
"id" : NumberLong(1085069192993353729),
"id_str" : "1085069192993353729",
"text" : "RT @wasimbrohi: Pakistan is beautiful country with full of natural colours and colourful cultures.\n#JoinPositivePakistan",
"truncated" : false,
"entities" : {
    "hashtags" : [ 
        {
            "text" : "JoinPositivePakistan",
            "indices" : [ 
                99, 
                120
            ]
        }
    ],
    "symbols" : [],
    "user_mentions" : [ 
        {
            "screen_name" : "wasimbrohi",
            "name" : "Wasim Brohi",
            "id" : 396599753,
            "id_str" : "396599753",
            "indices" : [ 
                3, 
                14
            ]
        }
    ],
    "urls" : []
},

我尝试了以下查询,但无法获取

db.getCollection('testdata').find({"entities":{"hashtags":[{"text":"JoinPositivePakistan"}]}});

2 个答案:

答案 0 :(得分:1)

尝试搜索

{"entities.hashtags.text": "JoinPositivePakistan"}

您的查询在仅包含以下内容的数组中搜索子文档

{"text": "JoinPositivePakistan"}

答案 1 :(得分:1)

您可以使用以下查询直接搜索嵌入式文档。无论数组中有多少文档。

db.testdata.find({"entities.hashtags.text": "JoinPositivePakistan"})

运行它,结果正在等待您...