pymongo查询某字段是否存在

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient["gaokao"]
mycol = mydb["college"]    
colleges = mycol.find({'code':{'$exists': 'true'}},{ "_id": 0, "name": 1, "code": 1 })

在MongoDB管理程序中,{'$exists': 'true'}不需要使用括号。

添加新评论