我正在尝试从mongodb collection" posts"中检索所有这些记录。其中主题标签的数量大于0 我的代码是:
client = MongoClient()
db = client['test-database']
collection = db['test-collection']
posts = db.posts
for post in posts.find({"entities":{"hashtags":{"$not": {"$size": 0}}}}):
post
存储的数据格式为
{
"_id" : ObjectId("5502eeec63a9f07bf15c0c74"),
"entities" : {
"user_mentions" : [
{
"id" : 588587397,
"indices" : [
0,
10
],
"id_str" : "588587397",
"screen_name" : "MehrTarar",
"name" : "Mehr Tarar"
}
],
"symbols" : [],
"trends" : [],
"hashtags" : [],
"urls" : []
},
"country_code" : "IN",
"attributes" : {},
"id" : "03b7ea94ef68e9d7",
"name" : "Mylapore Tiruvallike"
}
可能会建议更正 python2.7 mongodb3.0
答案 0 :(得分:0)
您可以尝试在查询对象键中使用数字数组索引来确定数组大小,例如这会查找至少包含hashtag数组元素的所有帖子文档:
db.posts.find({"entities.hashtags.0": {$exists: true}})