mongodb

来源:互联网 发布:一千零一夜 淘宝 出品 编辑:程序博客网 时间:2024/06/05 09:23

Note that an ObjectId is not the same as its string representation:

>>> post_id_as_str = str(post_id)>>> posts.find_one({"_id": post_id_as_str}) # No result>>>

A common task in web applications is to get an ObjectId from the request URL and find the matching document. It’s necessary in this case to convert the ObjectId from a string before passing it tofind_one:

from bson.objectid import ObjectId# The web framework gets post_id from the URL and passes it as a stringdef get(post_id):    # Convert from string to ObjectId:    document = client.db.collection.find_one({'_id': ObjectId(post_id)})
0 0
原创粉丝点击