我想为mongo中的特定id返回一个名为list_followers的列表,该列表位于数据库中。但是,数据库的一些文档的列表大小为零。因此,我得到了list_followers的key_错误。在
list_of_ids = []
for cursor in collection.find({ "list_followers" : { "$exists":True}}):
id = cursor['_id']['uid']
list_of_ids.append(id)
followers = cursor['list_followers']
print len(followers)
print len(list_of_ids)
我怎样才能只查询列表跟随者大于零的游标?我试过{"list_followers":{"$gt":0}}
或{
您正在寻找$size运算符。您还应该在查询中使用
$exists
,因为没有它,它甚至可以匹配没有字段list_followers
的文档:您可以检查^{} 是否不等于0:
感谢@Christian p关于
exists
需求的注释。在{cd3>或者,可以检查第一个元素}:
^{pr2}$试试这个
相关问题 更多 >
编程相关推荐