我的pymongo数据库中有我正在查询的集合中的数据,但是运行查询时它返回0个结果。
在下面的代码中,计数给了我9个结果,但是返回给邀请的光标中有0个项目。
@app.route("/myinvites", methods=["GET"])
@login_required
def myinvites():
myid = current_user.get_id()
invites = db.invite.find({"user_id":ObjectId(myid)})
count = db.invite.find({"user_id":ObjectId(myid)}).count()
print(count)
print(invites.retrieved)
return render_template("myinvites.html", myinvites = invites)
输出如下:
9 0
我不知道发生了什么,这似乎很奇怪,因为我在其他视图中使用了相同类型的查询,并且可以正常工作。
答案 0 :(得分:1)
如果只想计数就可以使用
count = db.invite.count_documents({"user_id":ObjectId(myid)})
效率更高。
答案 1 :(得分:0)
您在编辑中提到的 .count()返回光标返回的记录数。