This article describes how to use python to query data by _ id in mongodb. The example shows how to use Python to operate ObjectId in different versions based on pymongo, for more information about how to use python to query data by _ id, see the following example. Share it with you for your reference. The specific analysis is as follows:
_ Id is the id automatically generated by mongodb and its type is ObjectId. Therefore, if you need to query through _ id in python, you need to convert the type.
If the Python version is earlier than 2.2, use the following statement to import ObjectId.
From pymongo. objectid import ObjectId
If the version number of pymongo is greater than 2.2, use the following statement.
From bson. objectid import ObjectId
The query code is as follows:
Collection. find_one ({'_ id': ObjectId ('50f0d76109f4ec148890ef1e ')})
I hope this article will help you with Python programming.