For those of you used to working with RDBMS, you will notice that a critical field is missing: the famous primary key. In MongoDB, when a document is inserted into the database, a unique key is generated and given the reserved field name _id. Here is an example drawn from the products collection:
"_id" : ObjectId("5c5f8e011a2656b4af405319")
It is an instance of BSON data type ObjectId, and is generated as follows:
- 4-byte UNIX timestamp
- 5-byte random value
- 3-byte counter (starting with a random value)
This means that, in MongoDB, there is no need to define a unique key; however, it is often convenient to store a unique key of your own creation that is somehow tied to the data entered into the collection. For our purposes, we will create a unique field, productKey, which is a condensed version of the product title.
It's interesting to note that because the ObjectId contains a UNIX timestamp, it's possible to use it to determine...