Insertion order vs. natural order?

865 views
Skip to first unread message

Adam Fields

unread,
Jun 3, 2012, 12:11:20 PM6/3/12
to mongod...@googlegroups.com
It seems that natural order is usually the same as insertion order, but not quite always.

What will cause natural order to not be insertion order?

Max Schireson

unread,
Jun 3, 2012, 1:02:30 PM6/3/12
to mongod...@googlegroups.com

Documents can move after they are inserted, for example if they outgrow their initial space allocation. Also when documents have moved, there are "holes" that other new docs may go into depending on their size.

-- Max

On Jun 3, 2012 9:11 AM, "Adam Fields" <fie...@street86.com> wrote:
It seems that natural order is usually the same as insertion order, but not quite always.

What will cause natural order to not be insertion order?

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Adam Fields

unread,
Jun 3, 2012, 1:16:53 PM6/3/12
to mongod...@googlegroups.com, mongod...@googlegroups.com


On Jun 3, 2012, at 1:02 PM, Max Schireson <mschi...@gmail.com> wrote:

Documents can move after they are inserted, for example if they outgrow their initial space allocation. Also when documents have moved, there are "holes" that other new docs may go into depending on their size.


I have documents that represent aggregate stats for the day, and only the current day's record is modified. I recently noticed after about 18 months that one of them got inserted out of order. Is there any way to tell if it got inserted into a hole? Is there any way to prevent that, since under normal circumstances, older records will not be modified?

(Yes, I know I can convert it to a capped collection - this is more curiosity than anything else.)

John Wood (maverin)

unread,
Jun 3, 2012, 1:18:00 PM6/3/12
to mongod...@googlegroups.com
Out of interest, other than doing an occasional manual compaction, how do you deal with fragmentation? Does it auto compact in the background at all? 

Max Schireson

unread,
Jun 3, 2012, 1:36:20 PM6/3/12
to mongod...@googlegroups.com

I am not the best expert in this area but...

We maintain free lists for re-use. Exponentially increasing bucket sizes. If new docs being inserted have a similar size distribution to the docs being deleted the space is reused pretty efficiently. To my knowledge other than re-use from the free lists there is no background compaction, though I have heard discussions about adding it.

I'll leave others to answer in more detail if that doesn't give you what you need.

-- Max

Scott Hernandez

unread,
Jun 3, 2012, 1:44:50 PM6/3/12
to mongod...@googlegroups.com
No, you cannot prevent that behavior on a normal capped collection or
you would be left with holes that never get reused.

If you need things in a specific order it is best to use an explicit sort.

Asya Kamsky

unread,
Jun 3, 2012, 2:59:54 PM6/3/12
to mongod...@googlegroups.com

> aggregate stats for the day, and only the current day's record is modified.

How is a record is modified?  If you imagine the document as BSON you  can see that some changes will increase the size of the document - those that do could necessitate a move.   If during those 18 months any document was changed that way (or maybe deleted if the size of documents somehow varied during the 18 months) this could have created the one out of order document you see.

Asya


On Sunday, June 3, 2012 11:16:53 AM UTC-6, Adam Fields wrote:


On Jun 3, 2012, at 1:02 PM, Max Schireson <mschi...@gmail.com> wrote:

Documents can move after they are inserted, for example if they outgrow their initial space allocation. Also when documents have moved, there are "holes" that other new docs may go into depending on their size.


I have documents that represent aggregate stats for the day, and only the current day's record is modified. I recently noticed after about 18 months that one of them got inserted out of order. Is there any way to tell if it got inserted into a hole? Is there any way to prevent that, since under normal circumstances, older records will not be modified?

(Yes, I know I can convert it to a capped collection - this is more curiosity than anything else.)

-- Max

On Jun 3, 2012 9:11 AM, "Adam Fields" <fie...@street86.com> wrote:
It seems that natural order is usually the same as insertion order, but not quite always.

What will cause natural order to not be insertion order?

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to

See also the IRC channel -- freenode.net#mongodb

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
Reply all
Reply to author
Forward
0 new messages