Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MongoDB order their docs in one collection? [duplicate]

Tags:

In my User collection, MongoDB usually orders each new doc in the same order I create them: the last one created is the last one in the collection. But I have detected another collection where the last one I created has the 6 position between 27 docs.

Why is that?

Which order follows each doc in MongoDB collection?

like image 708
Paola G Avatar asked Oct 08 '15 14:10

Paola G


People also ask

How does MongoDB order data?

MongoDB can perform sort operations on a single-field index in ascending or descending order. In compound indexes, the sort order determines whether the index can be sorted. The sort keys must be listed in the same order as defined in the index.

Does MongoDB return documents in order?

Documents are stored in natural order The documents are stored in descending order based on date. So the collection has 20140731 as the first document. Unless you are using a capped collection, there is no guarantee for the ordering of documents on disk (also referred to as natural order).

How do I duplicate a document in MongoDB?

To clone a document, hover over the desired document and click the Clone button. When you click the Clone button, Compass opens the document insertion dialog with the same schema and values as the cloned document. You can edit any of these fields and values before you insert the new document.


Video Answer


1 Answers

It's called 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 get them in the same order you inserted, but that's not guaranteed–as you noticed.

Return in Natural Order

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

Index Use

Queries that include a sort by $natural order do not use indexes to fulfill the query predicate with the following exception: If the query predicate is an equality condition on the _id field { _id: <value> }, then the query with the sort by $natural order can use the _id index.

MMAPv1

Typically, the natural order reflects insertion order with the following exception for the MMAPv1 storage engine. For the MMAPv1 storage engine, the natural order does not reflect insertion order if the documents relocate because of document growth or remove operations free up space which are then taken up by newly inserted documents.

Obviously, like the docs mentioned, you should not rely on this default order (This ordering is an internal implementation feature, and you should not rely on any particular structure within it.).

If you need to sort the things, use the sort solutions.


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 that:

db.mycollection.find().sort({ "name": 1 }) 
like image 71
Ionică Bizău Avatar answered Oct 14 '22 16:10

Ionică Bizău