If _id is automatically generated by mongo, just use _id. If _id is also generated by yourself, you can only define a sorting key yourself.
mongo's _id is similar to mysql's id, almost the same. For mysql, you can set mysql to auto-increment, and then sort according to this mongo _id also increases by default. MongoDB documents must have an _id key. The purpose is to confirm that each document in the collection can be uniquely identified. ObjectId is the default type of _id. ObjectId uses 12 bytes of storage space, each byte has two hexadecimal digits, and is a 24-bit string. 12-bit generation rules: [0,1,2,3] [4,5,6] [7,8] [9,10,11] Timestamp | Machine code | PID | Counter The first four digits are Timestamp can provide second-level uniqueness. The next three digits are the unique identifier of the host, usually the hash value of the machine’s hostname. The next two digits are the PID that generated the ObjectId, ensuring that the ObjectIds generated concurrently on the same machine are unique. The first nine digits ensure that the ObjectId generated by different processes on different machines at the same second is unique. The last three digits are self-increasing counters to ensure that the ObjectId generated by the same process in the same second is unique Basically the order in the file (so it is often the order of insert).
If _id is automatically generated by mongo, just use _id. If _id is also generated by yourself, you can only define a sorting key yourself.
mongo's _id is similar to mysql's id, almost the same.
For mysql, you can set mysql to auto-increment, and then sort according to this
mongo _id also increases by default.
MongoDB documents must have an _id key.
The purpose is to confirm that each document in the collection can be uniquely identified.
ObjectId is the default type of _id.
ObjectId uses 12 bytes of storage space, each byte has two hexadecimal digits, and is a 24-bit string.
12-bit generation rules:
[0,1,2,3] [4,5,6] [7,8] [9,10,11]
Timestamp | Machine code | PID | Counter
The first four digits are Timestamp can provide second-level uniqueness.
The next three digits are the unique identifier of the host, usually the hash value of the machine’s hostname.
The next two digits are the PID that generated the ObjectId, ensuring that the ObjectIds generated concurrently on the same machine are unique. The first nine digits ensure that the ObjectId generated by different processes on different machines at the same second is unique.
The last three digits are self-increasing counters to ensure that the ObjectId generated by the same process in the same second is unique
Basically the order in the file (so it is often the order of insert).
Just reverse the _id of the objects implicitly created by mongodb, that’s what you want
MongoDb original statement: {$sort: {FieldName: 1 or -1}}
1: means ascending order
-1: means descending order