mongodb $总是返回0

时间:2012-09-13 02:27:52

标签: mongodb pymongo

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

{'followers':
        {
           '123':1
           '123':2
           '123':3
         }
}

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

cursor = fols.find()
cursor.count()
>>3

工作正常。现在:

cursor = fols.find({'followers':{'123':1}})
cursor.count()
>>1

再次正常工作。但如果我尝试:

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

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

2 个答案:

答案 0 :(得分:24)

如果您未匹配完整对象,则需要使用dot notation对嵌入对象使用运算符。所以在这种情况下:

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

答案 1 :(得分:5)

尝试使用点语法:

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

但也请看上面的评论。您不能在(子)文档中多次使用相同的密钥。