Pymongo检查文档中是否存在字段

时间:2017-10-19 08:09:31

标签: python mongodb pymongo

我有一个帖子文件。我想在嵌入式文档中添加注释。有些文件没有评论字段。

{
    "_id" : 6,
    "title" : "Emacs tutorial",
    "updatedate" : ISODate("2017-10-18T19:05:08.555Z"),
    "content" : "Welcome to Emacs tutorial\n",
    "comments" : {
        "date" : "2016-04-20",
        "content" : "first comment",
        "_id" : 122
    }
}

我想做的是检查当前文档是否包含“评论”字段。

这是我到目前为止所尝试的内容:

document = mongo.db.posts

result = document.find(
    { '_id' : int(number) },
    { "comments": { '$exists': True, '$ne': False } })

print(result)

if result:
    print ('I find the comments field')
else:
    print ('I shouldn't find')

但我知道打印结果会返回一个pymongo.cursor.Cursor对象。

0 个答案:

没有答案
相关问题