7

我有一个数据库集合(名为 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。

4

2 回答 2

27

当您不匹配完整的对象时,您需要使用点表示法来对嵌入对象使用运算符。所以在这种情况下:

cursor = fols.find({'followers.123':{'$exists': True}})
于 2012-09-13T02:43:25.630 回答
5

尝试点语法:

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

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

于 2012-09-13T02:44:36.223 回答