mongodb $ exists总是返回0 [英] mongodb $exists always returning 0
本文介绍了mongodb $ exists总是返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数据库集合(名为fols):
I have a database collection (named fols) like so:
{'followers':
{
'123':1
'123':2
'123':3
}
}
如果我运行查询(使用pymongo):
If I run the query (Using pymongo):
cursor = fols.find()
cursor.count()
>>3
工作正常.现在:
cursor = fols.find({'followers':{'123':1}})
cursor.count()
>>1
再次正常工作.但是,如果我尝试:
Again works fine. BUT if I try:
cursor = fols.find({'followers':{'123':{'$exists': True}}})
cursor.count()
>> 0
即使有3条记录,它也会返回0.
It returns 0 even though there are 3 records.
推荐答案
When you're not matching a complete object you need to use dot notation to use an operator against an embedded object. So in this case:
cursor = fols.find({'followers.123':{'$exists': True}})
这篇关于mongodb $ exists总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文