In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Navicat Premium is a database development tool that allows you to connect to both MySQL,MariaDB,MongoDB,SQL Server,Oracle,PostgreSQL and SQLite databases from a single application. Compatible with cloud databases such as Amazon RDS,Amazon Aurora,Amazon Redshift,Microsoft Azure,Oracle Cloud,Google Cloud and MongoDB Atlas.
If you open any document in the MongoDB database, you will notice the _ id field:
In fact, ObjectId / _ id is the only field in each MongoDB document. In today's article, we will explore its meaning and its importance to MongoDB databases.
The structure of ObjectId
As a quick, development summary, here are some of the main features of _ id:
_ id is the primary key of documents in the collection; with it, documents (records) can be distinguished from each other.
_ id automatic indexing. Specify {_ id:
By default, the _ id field is of type ObjectID, which is one of the BSON types of MongoDB. If desired, the user can also override _ id to a value other than ObjectID.
ObjectID is 12 bytes long and consists of a chain of 2-4 bytes. Each chain represents and specifies a specific aspect of the document identity. The following values form a complete combination of 12 bytes:
A 4-byte value that represents the number of seconds since the age of Unix
A 3-byte machine identifier
A 2-byte process ID
A 3-byte counter that begins with a random value
In general, you don't have to worry about generating ObjectID. If no _ id value is assigned to the document, MongoDB automatically generates one.
Create a new ObjectId
If you want to generate a new ObjectId yourself, you can use the following code:
NewObjectId = ObjectId ()
You can also type it directly in the Navicat editor.
This will generate a unique _ id, for example:
ObjectId ("5349b4ddd2781d08c09890f3")
Alternatively, you can provide a 12-byte ID:
MyObjectId = ObjectId ("5349b4ddd2781d08c09890f4")
Timestamp when the document was created
Because _ id ObjectId stores a 4-byte timestamp by default, in most cases, you do not need to store the creation time of any documents. You can use the getTimestamp method to get the creation time of the document:
ObjectId ("5349b4ddd2781d08c09890f4"). GetTimestamp ()
This will return the creation time of this document in ISO date format
ISODate ("2019-09-12T30:39:17Z")
Convert ObjectId to String
In some cases, you may need an ObjectID value in string format. To convert ObjectId to a string, use the following code:
NewObjectId.str
The above code returns the string format of Guid:
5349b4ddd2781d08c09890f3
Document classification
Because each ObjectId contains a timestamp, you can sort documents by _ id or by creation time. It is important to note, however, that this sorting method does not represent strict or precise sorting, as other components of ID may work, causing the command to reflect not only the creation time, but also other variables.
Change ObjectId
The _ id field is basically immutable, so after creating the document, by definition, it has been assigned _ id, and the ID cannot be changed. That said, _ id can be overwritten when a new document is inserted. It may be useful to override the _ id field of a document, but when you do so, it is your responsibility to ensure that the value of each document is unique.
Conclusion
The _ id field of MongoDB plays a vital role in every MongoDB collection. Therefore, knowing how to create it and when to overwrite it is useful for managing collections.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.