1. Object ID generation each mongoDB document requires a primary key. It must be unique to all documents in each set and the primary key is stored in the document _ id field. Consists of 12 characters; 4c291856238d3b19b20000014 byte timestamp machine ID process ID counter 3333 II. BSONBSON is used to mark the document in mongodb
1. Object ID generation each mongoDB document requires a primary key. It must be unique to all documents in each set and the primary key is stored in the document _ id field. It consists of 12 characters; 4c291856 238d3b 19b2 000001 4-byte timestamp machine ID process ID counter 3333 2. BSON is used in mongodb to mark the document
I. Generation of object IDS
Each mongoDB document requires a primary key, which must be unique to all documents in each set, and the primary key is stored in the document _ id field. It consists of 12 characters;
4c291856 238d3b 19b2 000001
4-byte timestamp: Machine ID process ID counter 3333
Ii. BSON
BSON is the binary format used to mark documents in mongodb. It is both a storage format and a command format. All documents are stored on the disk as bson, and all queries and commands are specified in the bson document.
Db. users. find ({_ id: ObjectId ('4c291856238d3b19b2000001 ')})
Db. users. find ({_ id: '4c291856238d3b19b2000001 '})
The results of the above two queries are completely different. Only one of them can query the match _ id field, this depends entirely on whether the document in the users set stores the BSON Object ID or the BSON string that marks the ID hex.
Iii. Restrictions on aggregate commands
In terms of practicality, distinct and group have a large limit, and they cannot return more than 16 M results. The limit of 16 m is not the threshold value imposed by these commands, which is the size of all the initial query results. If distinct and group cannot process the result set of your set, they can only be replaced by map-reduce, and their results can be saved in non-inline return of the set.
Iv. Atomic Document Processing
We know that mongodb is not good at processing things, but what if users need to perform queries and updates at the same time? You certainly don't want to miss a tool, that is, the findAndModify command of mongodb. This command allows atomic updates to documents and returns results in the same call.
Db. collections. findAndModify (
{
Query :{}, update :{}, new: true or false
}
)
By default, the findandmodify command returns the pre-update document. If the modified document is returned, set new to false.
5. Use $ unset for Arrays
Note that the result of using $ unset on a single array element may be different from what you imagined. The result is only to set the element value to null, instead of deleting the entire element. To completely delete an array element, use the $ pull and $ pop operators.
6. Differences between $ addToSet and $ push
Both functions add a value to the array. However, there is a difference between the two. $ addToSet adds only one value if it does not exist. For example:
Tags = ["tools", "garden"]
If you execute db. collection. update ({},{ $ push: {tag: tools}), the result is ["tools", "garden", "tools"].
If the db. collection. update ({},{$ addToSet: {tag: tools }}) result is not changed
VII. Create a sparse Index
Only the document with the value of the index key appears in the sparse index. To create a sparse index, specify {sparse: true. For example:
Db. product. ensureIndex ({sku: 1}, {unique: true, sparse: true })
The Sku may have a null document.
8. Be careful when declaring Indexes
Because creating an index is relatively simple, it is easy to create an index accidentally. If the dataset is large, it takes a long time to build the index. And there is no way to plant them. At the same time, it is better to sort indexes first for greater efficiency.
9. Use Explain (true) to query the execution plan in detail
User db. collection. find (condition). explain (true)
10. Optimistic lock
Optimistic Locking is concurrency control, which ensures that the device is thoroughly updated without locking records. To understand it, the simplest way is to imagine a Wi-Fi. Multiple users can edit a page at the same time. But you certainly don't want the user to edit and update an expired page, so you can use the optimistic lock protocol. When the user tries to save their changes, a timestamp is added to the update operation. If the value is earlier than the version saved recently on the page, the user will not be updated.
This is used in mongodb $ inc execution