How does MongoDB order its documents in one collection?

In my collection, User MongoDB usually orders each new document in the same order that I create it: the last created is the last in the collection. But I found another collection in which the last one I created has 6 positions between 27 documents.

Why is this?

What order follows each document in the MongoDB collection?

+6
source share
4 answers

It is called the natural order :

natural order

The order in which the database refers to documents on disk. This is the default sort order. See $natural and Return in Natural Order .

This confirms that in general you receive them in the same order that you inserted, but this is not guaranteed - as you noticed.

Return in kind

The $natural parameter returns elements according to their natural order in the database. This ordering is an internal implementation function, and you should not rely on any particular structure within it.

Index usage

Queries containing the $natural collation do not use indexes to execute the query predicate using the following exception: if the query predicate is an equality condition in the _id { _id: <value> } field, then the query with the $natural collation order can use the _id index.

MMAPv1

Typically, the natural order reflects the insertion order in the following exception for the MMAPv1 storage engine. For the MMAPv1 storage engine, the natural order does not reflect the insertion order if the documents are moved due to the growth of the document or the deletion operations are removed that are then examined by newly inserted documents.

Obviously, as in the mentioned documents, you should not rely on this default order (this ordering is an internal implementation function, and you should not rely on any specific structure within it.). p>

If you need to sort things, use sorting methods .


Basically, the following two calls should return documents in the same order (since the default order is $natural ):

 db.mycollection.find().sort({ "$natural": 1 }) db.mycollection.find() 

If you want to sort by another field (e.g. name ), you can do this:

 db.mycollection.find().sort({ "name": 1 }) 
+9
source

MongoDB does not "order" documents at all unless you ask for it.

The main insert will create an ObjectId in the primary key _id , unless you tell it otherwise. This ObjectId value is a special value with "monotonic" or "constantly increasing" properties, which means that every value created is guaranteed to be greater than the last.

If you want to sort, do an explicit sort:

  db.collection.find().sort({ "_id": 1 }) 

Or "natural" sort in the order stored on disk:

 db.collection.find().sort({ "$natural": 1 }) 

This is largely a standard, unless otherwise indicated, or a query criterion that determines the sort order, selects the "index". But you can use this to โ€œforceโ€ this order if the query criteria selected an index that was otherwise sorted.

MongoDB documents "move" during growing, so the _id order _id not always explicitly the same order as when receiving documents.

+2
source

I could learn more about this through the Return in Natural Order link provided by Ionicฤƒ Bizau .

"$ natural parameter returns the elements according to their natural order in the database. This ordering is an internal implementation function, and you should not rely on any particular structure within it.

Typically, the natural order reflects the insertion order in the following exception for the MMAPv1 storage engine. For the MMAPv1 storage engine, the natural order does not reflect the insertion order if documents are moved due to document growth or deletion of operations, freeing up space that is then processed by newly inserted documents.

+2
source

For performance reasons, MongoDB never crashes a document on the hard drive.

When you start with an empty collection and start pasting the document after the document into it, mongoDB will place them sequentially on disk.

But what happens when you update a document, and now it takes up more space and no longer fits into its previous position, without overlapping the next? In this case, MongoDB will remove it and re-add it as new at the end of the collection file.

Your collection file has a hole in unused space. This is a pretty waste, isn't it? That is why the following document, which is inserted and small enough to fit into this hole, will be inserted into this hole. Probably what happened in the case of your second collection.

Bottom line: Never rely on documents returned in the order of placement. When you take care of an order, always sort your results.

0
source

All Articles