Hướng dẫn get id from objectid mongodb python - lấy id từ objectid mongodb python

16

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang cố gắng lấy ID từ tài liệu mà tôi có ở MongoDB, sử dụng pymongo.

Đây là mã của tôi:

docQuery = db.doctors.find({"email":doc_mail})
doc_id = docQuery[0]["_id"]["$oid"]

Tôi cũng đã thử điều này:

 doc_id = docQuery[0]["_id"]

Không ai trong số họ hoạt động!

Hướng dẫn get id from objectid mongodb python - lấy id từ objectid mongodb python

Ivanleoncz

8.0574 Huy hiệu vàng53 Huy hiệu bạc48 Huy hiệu đồng4 gold badges53 silver badges48 bronze badges

Đã hỏi ngày 26 tháng 6 năm 2015 lúc 16:16Jun 26, 2015 at 16:16

7

Mặc dù cách tiếp cận thứ hai của bạn nên hoạt động, docQuery là đối tượng loại Cursor. Cách tốt nhất là lặp lại nó như:

for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')

Hoặc nếu chỉ có một đối tượng, thì hãy sử dụng find_one như:

itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')

Đã trả lời ngày 26 tháng 6 năm 2015 lúc 16:35Jun 26, 2015 at 16:35

Hướng dẫn get id from objectid mongodb python - lấy id từ objectid mongodb python

Salmanwahedsalmanwahedsalmanwahed

9.0507 Huy hiệu vàng31 Huy hiệu bạc54 Huy hiệu đồng7 gold badges31 silver badges54 bronze badges

0

Trong pymongo, bạn có thể sử dụng [''] ký hiệu này để truy cập vào tài sản cụ thể. Thí dụ -
Example -

cursor = collection.find({})
for document in cursor:
        print document['_id']

Đã trả lời ngày 26 tháng 7 năm 2018 lúc 11:15Jul 26, 2018 at 11:15

MongoDB là cơ sở dữ liệu định hướng tài liệu NoQuery. Trong MongoDB, dữ liệu được lưu trữ trong cấu trúc tài liệu giống JSON thay vì lưu trữ dữ liệu theo các cột và hàng. MongoDB linh hoạt để lưu trữ dữ liệu bán cấu trúc và không cấu trúc. MongoDB được sử dụng để lưu trữ dữ liệu khối lượng lớn và có thể mở rộng theo chiều ngang. is NoSQL Document-oriented database. In MongoDB, the data are stored in a JSON-like document structure instead of storing data in columns and rows manner. MongoDB is flexible for storing semi-structured and unstructured data. MongoDB is used for high-volume data storage and can scale horizontally.

ObjectID: Lớp ObjectID là loại BSON nhị phân 12 byte, trong đó 4 byte dấu thời gian của sáng tạo, 5 byte có giá trị ngẫu nhiên và 3 byte của bộ đếm tăng. ObjectID là khóa chính mặc định cho các tài liệu MongoDB và thường được tìm thấy trong trường _id _id.ObjectId class is a 12-byte binary BSON type, in which 4 bytes of the timestamp of creation, 5 bytes of a random value, and 3 bytes of incrementing counter. ObjectId is the default primary key for MongoDB documents and is usually found in “_id” field.

eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }

Pymongo: Pymongois Một trình điều khiển Python bản địa cho MongoDB. Nó cho phép tương tác với cơ sở dữ liệu MongoDB thông qua Python.PyMongois a native Python driver for MongoDB. It allows interaction with MongoDB Database through Python.

Bước 1: Đảm bảo khởi động cơ sở dữ liệu MongoDB cục bộ trên cổng mặc định (27017). Kết nối với MongoDB, truy cập cơ sở dữ liệu và thu thập các đối tượng.Make sure to start the MongoDB database locally on the default port (27017). Connecting to MongoDB, accessing the database, and collecting objects.

Python3

 doc_id = docQuery[0]["_id"]
0
 doc_id = docQuery[0]["_id"]
1
 doc_id = docQuery[0]["_id"]
2
 doc_id = docQuery[0]["_id"]
3

 doc_id = docQuery[0]["_id"]
0
 doc_id = docQuery[0]["_id"]
5
 doc_id = docQuery[0]["_id"]
2
 doc_id = docQuery[0]["_id"]
7

 doc_id = docQuery[0]["_id"]
8
 doc_id = docQuery[0]["_id"]
9
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
0
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
1
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
2223
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
4

for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
5
 doc_id = docQuery[0]["_id"]
9
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
7
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
8
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
9

itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
0
 doc_id = docQuery[0]["_id"]
9
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
2
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
3
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
9

Bước 2: Truy vấn MongoDB với find_one ().Querying MongoDB with find_one().

Syntax:

find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments

Ví dụ 1: Trong ví dụ này, chúng tôi đã lưu trữ thể hiện đối tượng ObjectID, sau đó sử dụng thể hiện đối tượng được lưu trữ để tìm kiếm tài liệu.In this example, we have already stored the ObjectId object instance, then use the stored object instance to search for the document.

Python3

itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
5
 doc_id = docQuery[0]["_id"]
9
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
7
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
8
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
9
cursor = collection.find({})
for document in cursor:
        print document['_id']
0__222222222

cursor = collection.find({})
for document in cursor:
        print document['_id']
6

Output:

Ví dụ 2: Trong ví dụ này, chúng ta có chuỗi Hex không phải là đối tượng của ObjectID. Chúng tôi nhập ObjectID từ thư viện BSON để tìm kiếm tài liệu.In this example, we have the hex string not an object of ObjectId. We import ObjectId from the bson library to search the document.

Python3

cursor = collection.find({})
for document in cursor:
        print document['_id']
7
 doc_id = docQuery[0]["_id"]
9
cursor = collection.find({})
for document in cursor:
        print document['_id']
9

itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
5
 doc_id = docQuery[0]["_id"]
9
eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
2
cursor = collection.find({})
for document in cursor:
        print document['_id']
7
for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
4

eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
5
eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
6
eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
7

eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
5
eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
6
find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
0
cursor = collection.find({})
for document in cursor:
        print document['_id']
7
find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
2

find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
3
cursor = collection.find({})
for document in cursor:
        print document['_id']
7
find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
5

Output:

Hướng dẫn get id from objectid mongodb python - lấy id từ objectid mongodb python

Ví dụ 3: Trong ví dụ này, chúng tôi tiến lên tìm kiếm bằng cách chỉ định các dự báo đó là trường nào chúng tôi muốn đưa/loại trừ trong tập kết quả. Hình chiếu dưới đây sẽ trả về toàn bộ tài liệu mà không cần trường _id.In this example, we advance the search by specifying projections that is which field we want to include/exclude in the result set. The below projection will return the entire document without the “_id” field.

Python3

find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
6
 doc_id = docQuery[0]["_id"]
9
find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
8
eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
6
find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
0
cursor = collection.find({})
for document in cursor:
        print document['_id']
7docQuery2

docQuery3

 doc_id = docQuery[0]["_id"]
9
find_one(filter=None, *args, **kwargs)
filter(optional): query filter that selects which documents should be included in the result set.
*args (optional): any additional positional arguments
*kwargs (optional): any additional keyword arguments
8
eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }
6
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
9docQuery8__

Cursor0docQuery3

for itm in db.doctors.find({"email":doc_mail}):
   print itm.get('_id')
4

Output:


Làm cách nào để tìm ID đối tượng trong MongoDB?

valueOf () trả về giá trị của objectId () dưới dạng chuỗi thập lục phân chữ thường. Giá trị này là thuộc tính STR của đối tượng ObjectID (). Returns the value of the ObjectId() as a lowercase hexadecimal string. This value is the str attribute of the ObjectId() object.

MongoDB có tạo ID không?

Theo mặc định, MongoDB tạo ra một mã định danh ObjectID duy nhất được gán cho trường _ID trong một tài liệu mới trước khi viết tài liệu đó vào cơ sở dữ liệu.Trong nhiều trường hợp, các định danh duy nhất mặc định được chỉ định bởi MongoDB sẽ đáp ứng các yêu cầu ứng dụng.. In many cases the default unique identifiers assigned by MongoDB will meet application requirements.

ID đối tượng MongoDB là gì?

ID đối tượng được coi là khóa chính trong bất kỳ bộ sưu tập MongoDB nào.Nó là một định danh duy nhất cho mỗi tài liệu hoặc bản ghi.Cú pháp: ObjectId ().Một ObjectID là một chuỗi thập lục phân loại BSON 12 byte có cấu trúc như trong ví dụ dưới đây.Ví dụ: ObjectID ("6009C0EEE65F6DCE28FB3E50")a unique identifier for each document or record. Syntax: ObjectId(). An ObjectId is a 12-byte BSON type hexadecimal string having the structure as shown in the example below. Example: ObjectId("6009c0eee65f6dce28fb3e50")

_Id của loại objectid là gì?

Trường _id có thể được sử dụng ở bất kỳ định dạng nào và định dạng mặc định là ObjectID của tài liệu.Một ObjectID là một trường 12 byte của loại BSON.4 byte đầu tiên đại diện cho dấu thời gian UNIX của tài liệu.3 byte tiếp theo là ID máy mà máy chủ MongoDB đang chạy.a 12-byte Field Of BSON type. The first 4 bytes representing the Unix Timestamp of the document. The next 3 bytes are the machine Id on which the MongoDB server is running.