24

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?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Paola G
  • 798
  • 1
  • 6
  • 13

4 Answers4

33

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 })
Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
10

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

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

But what happens when you update a document and it now takes more space and doesn't fit into its old position anymore without overlapping the next? In that case MongoDB will delete it and re-append it as a new one at the end of the collection file.

Your collection file now has a hole of unused space. This is quite a waste, isn't it? That's why the next document which is inserted and small enough to fit into that hole will be inserted in that hole. That's likely what happened in the case of your second collection.

Bottom line: Never rely on documents being returned in insertion order. When you care about the order, always sort your results.

Philipp
  • 67,764
  • 9
  • 118
  • 153
4

MongoDB does not "order" the documents at all, unless you ask it to.

The basic insertion will create an ObjectId in the _id primary key value unless you tell it to do otherwise. This ObjectId value is a special value with "monotonic" or "ever increasing" properties, which means each value created is guaranteed to be larger than the last.

If you want "sorted" then do an explicit "sort":

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

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

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

Which is pretty much the standard unless stated otherwise or an "index" is selected by the query criteria that will determine the sort order. But you can use that to "force" that order if query criteria selected an index that sorted otherwise.

MongoDB documents "move" when grown, and therefore the _id order is not always explicitly the same order as documents are retrieved.

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
2

I could find out more about it thanks to the link Return in Natural Order provided by Ionică Bizău.

"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.

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."

Community
  • 1
  • 1
Paola G
  • 798
  • 1
  • 6
  • 13