def get_registered_user():
users = collection_users.find({name:'john'}, {'regDate': regDate}).count()
pprint.pprint("users:" + str(users))
在 mongoDB python 中使用 count() 方法时,我收到警告“DeprecationWarning: count is deprecated。改用 Collection.count_documents。”
Collection.count_documents()
只接受一个查询。如何同时传递以上两个查询?请帮忙。
答案 0 :(得分:0)
这里,find()
方法接受两个逗号分隔的条件。但 count_documents()
不接受多个条件。
为此可以使用 $and
运算符。 $and
运算符使用逻辑 AND 连接两个或多个查询,并返回匹配所有条件的文档。
我们可以将两个条件组合为
def get_registered_user():
criteria = { '$and' : [ {name:'john'},{'regDate': regDate} ] }
users = collection_users.count_documents(criteria)
pprint.pprint("users:" + str(users))