mongodb$始终存在并返回0

mongodb$始终存在并返回0,mongodb,pymongo,Mongodb,Pymongo,我有一个数据库集合(名为fols),如下所示: 如果我运行查询(使用pymongo): 很好。现在: cursor = fols.find({'followers':{'123':1}}) cursor.count() >>1 同样也很好。但如果我尝试: cursor = fols.find({'followers':{'123':{'$exists': True}}}) cursor.count() >> 0 即使有3条记录,它也会返回0。当您没有匹配完整的对象时,

我有一个数据库集合(名为fols),如下所示:

如果我运行查询(使用pymongo):

很好。现在:

cursor = fols.find({'followers':{'123':1}})
cursor.count()
>>1
同样也很好。但如果我尝试:

cursor = fols.find({'followers':{'123':{'$exists': True}}})
cursor.count()
>> 0

即使有3条记录,它也会返回0。

当您没有匹配完整的对象时,您需要使用运算符对嵌入对象进行匹配。因此,在这种情况下:

cursor = fols.find({'followers.123':{'$exists': True}})
尝试点语法:

cursor = fols.find({'followers.123': {'$exists': True}})

但也可以看到我上面的评论。在一个(子)文档中不能多次使用同一个键。

您可以澄清一下:您是否有一个包含三个文档的集合
followers
:{'123':1},{'123':2},{'123':3},或者您是否有一个集合
fols
包含一个包含三个子文档的文档:{'followers':{'123':1},{'123':2},{'123':3}?因为键“123”将被覆盖,只剩下一个副本。我有一个集合fols,其中包含一个文档“followers”,其中包含其他值。我可以确认它没有像我那样被覆盖:fols.find()。它返回我的所有3个文档。如果fols集合中只有一个文档,那么fols.find()应该只返回一个文档,而不是3个。我在mongo shell:db.fols.insert({'followers':{'123':1,'123':2,'123':3}}})和db.fols.find()中尝试了这一点,并使用了{“_id”:ObjectId(“50514948484752df6214”),“followers:{“123”:3}。数组的say key 123”是无意义的。这将折叠成一个数组,其中ony key-value对隐藏了另外两个。
cursor = fols.find({'followers.123':{'$exists': True}})
cursor = fols.find({'followers.123': {'$exists': True}})