Nhận ObjectId MongoDB

Chúng ta đã sử dụng MongoDB Object Id trong tất cả các chương trước. Trong chương này, chúng ta sẽ tìm hiểu cấu trúc của ObjectId

ObjectId là loại BSON 12 byte có cấu trúc như sau -

  • 4 byte đầu tiên biểu thị số giây kể từ kỷ nguyên unix
  • 3 byte tiếp theo là định danh máy
  • 2 byte tiếp theo bao gồm id quá trình
  • 3 byte cuối cùng là giá trị bộ đếm ngẫu nhiên

MongoDB sử dụng ObjectIds làm giá trị mặc định của trường _id của mỗi tài liệu, được tạo trong khi tạo bất kỳ tài liệu nào. Sự kết hợp phức tạp của ObjectId làm cho tất cả các trường _id trở nên độc nhất

Tạo ObjectId mới

Để tạo một ObjectId mới, hãy sử dụng đoạn mã sau -

>newObjectId = ObjectId()

Câu lệnh trên trả về id được tạo duy nhất sau đây -

ObjectId("5349b4ddd2781d08c09890f3")

Thay vì MongoDB tạo ObjectId, bạn cũng có thể cung cấp id 12 byte -

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")

Tạo dấu thời gian của tài liệu

Vì _id ObjectId theo mặc định lưu trữ dấu thời gian 4 byte, nên trong hầu hết các trường hợp, bạn không cần lưu trữ thời gian tạo của bất kỳ tài liệu nào. Bạn có thể lấy thời gian tạo tài liệu bằng phương thức getTimestamp -

>ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()

Điều này sẽ trả về thời gian tạo tài liệu này ở định dạng ngày ISO -

ISODate("2014-04-12T21:49:17Z")

Chuyển đổi ObjectId thành Chuỗi

Trong một số trường hợp, bạn có thể cần giá trị của ObjectId ở định dạng chuỗi. Để chuyển đổi ObjectId thành chuỗi, hãy sử dụng đoạn mã sau –

MongoDB là cơ sở dữ liệu định hướng tài liệu NoSQL. Trong MongoDB, dữ liệu được lưu trữ trong cấu trúc tài liệu giống như JSON thay vì lưu trữ dữ liệu theo kiểu cột và hàng. MongoDB linh hoạt để lưu trữ dữ liệu bán cấu trúc và phi 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

ID đối tượng. Lớp ObjectId là loại BSON nhị phân 12 byte, trong đó 4 byte dấu thời gian tạo, 5 byte giá trị ngẫu nhiên và 3 byte bộ đếm tăng dần. 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”

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

PyMongo. PyMongo là trình điều khiển Python gốc cho MongoDB. Nó cho phép tương tác với Cơ sở dữ liệu MongoDB thông qua Python

Bước 1. Đảm bảo khởi động cục bộ cơ sở dữ liệu MongoDB 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

Python3




>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
2

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
3
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
4
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
5
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
6

 

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
7

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
3
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
9
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
5
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
1

 

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

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
4
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
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
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
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
8
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
9
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
20

 

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
21

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
22
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
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
24
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
25
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
26

 

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
27

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
28
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
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
30
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
31
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
26

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

cú pháp

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ữ đối tượng ObjectId, sau đó sử dụng đối tượng được lưu trữ để tìm kiếm tài liệu

Python3




>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
33

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
34

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
35
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
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
37
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
38
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
39
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
40
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
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
42
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
39
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
44
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
45

 

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
46

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
47

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
48

đầu ra

Nhận ObjectId MongoDB

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

Python3




>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
49

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
50

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
51

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
52
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
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
54

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
35
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
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
57____252
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
20

 

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
60
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
61____262

 

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
63

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
60
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
61____266
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
52
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
68

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
69
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
52____271

đầu ra

Nhận ObjectId MongoDB

ví dụ 3. Trong ví dụ này, chúng tôi tiến hành tìm kiếm bằng cách chỉ định các phép chiếu là trường nào chúng tôi muốn bao gồm/loại trừ trong tập kết quả. Phép chiếu bên dưới sẽ trả về toàn bộ tài liệu mà không có trường “_id”

ObjectId trong MongoDB là gì?

ID đối tượng là Trường loại BSON 12 byte . 4 byte đầu tiên biểu thị 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. 2 byte tiếp theo là process id. Trường cuối cùng là 3 byte được sử dụng để tăng objectid.

MongoDB tạo ObjectId như thế nào?

ObjectID được được trình điều khiển cơ sở dữ liệu tạo tự động và sẽ được gán cho trường _id của từng tài liệu. ObjectID có thể được coi là duy nhất trên toàn cầu cho tất cả các mục đích thực tế. ObjectID mã hóa dấu thời gian của thời gian tạo, dấu này có thể được sử dụng cho các truy vấn hoặc để sắp xếp theo thời gian tạo.

Tại sao ID MongoDB là ObjectId?

MongoDB sử dụng ObjectIds làm giá trị mặc định của trường _id của mỗi tài liệu, được tạo trong quá trình tạo bất kỳ tài liệu nào . ID đối tượng được coi là khóa chính trong mọi bộ sưu tập MongoDB. Nó là một mã định danh duy nhất cho mỗi tài liệu hoặc hồ sơ. cú pháp. ObjectId(

MongoDB ObjectId có phải là UUID không?

Theo mặc định, trình điều khiển Java MongoDB tạo ID thuộc loại ObjectId. Đôi khi, chúng tôi có thể muốn sử dụng một loại dữ liệu khác làm mã định danh duy nhất của một đối tượng, chẳng hạn như UUID. Tuy nhiên, trình điều khiển Java MongoDB không thể tự động tạo UUID .