Skip to content Skip to sidebar Skip to footer

Can Mongodb And Its Drivers Preserve The Ordering Of Document Elements

I am considering using MongoDB to store documents that include a list of key/value pairs. The safe but ugly and bloated way to store this is as [ ['k1' : 'v1'] , ['k2' : 'v2'], .

Solution 1:

From Version 2.6 on, MongoDB preserves the order of fields where possible. However, the _id field always comes first an renaming fields can lead to re-ordering. However, I'd generally try not to rely on details like this. As the original question mentions, there are also additional layers to consider which each must provide some sort of guarantee for the stability of the order...

Original Answer:

No, MongoDB does not make guarantees about the ordering of fields:

"There is no guarantee that the field order will be consistent, or the same, after an update."

In particular, in-place updates that change the document size will usually change the ordering of fields. For example, if you $set a field whose old value was of type number and the new value is NumberLong, fields usually get re-ordered.

However, arrays preserve ordering correctly:

[ {'key1' : 'value1'}, {'key2' : 'value2'}, ... ]

I don't see why this is "ugly" and "bloated" at all. Storing a list of complex objects couldn't be easier. However, abusing objects as lists is definitely ugly: Objects have associative array semantics (i.e. there can only be one field of a given name), while lists/arrays don't:

// not ok:
db.foo2.insert({"foo" : "bar", "foo" : "lala" });
db.foo2.find();
{ "_id" : ObjectId("4ef09cd9b37bc3cdb0e7fb26"), "foo" : "lala" }

// a list can do that
db.foo2.insert({ 'array' : [ {'foo' : 'bar'}, { 'foo' : 'lala' } ]});
db.foo2.find();
{ "_id" : ObjectId("4ef09e01b37bc3cdb0e7fb27"), "array" : 
      [ { "foo" : "bar" }, { "foo" : "lala" } ] }

Keep in mind that MongoDB is an object database, not a key/value store.

Solution 2:

As of Mongo 2.6.1, it DOES keep the order of your fields:

MongoDB preserves the order of the document fields following write operations except for the following cases:

  • The _id field is always the first field in the document.
  • Updates that include renaming of field names may result in the reordering of fields in the document.

http://docs.mongodb.org/manual/release-notes/2.6/#insert-and-update-improvements

Solution 3:

One of the pain points of this is comparing documents to one another in the shell.

I've created a project that creates a custom mongorc.js which sorts the document keys by default for you when they are printed out so at least you can see what is going on clearly in the shell. It's called Mongo Hacker if you want to give it a whirl.

Solution 4:

Though it's true that, as of Mongo 2.6.1, it does preserve order, one should still be careful with update operations.

mattwad makes the point that updates can reorder things, but there's at least one other concern I can think of.

For example $addToSet:

https://docs.mongodb.com/manual/reference/operator/update/addToSet/

$addToSet when used on embedded documents in an array is discussed / exemplified here: https://stackoverflow.com/a/21578556/3643190

In the post, mnemosyn explains how $addToSet disregards the order when matching elements in its deep value by value comparison.

($addToSet only adds records when they're unique)

This is relevant if one decided to structure data like this:

[{key1:v1, key2:v2}, {key1:v3, key2:v4}]

With an update like this (notice the different order on the embedded doc):

db.collection.update({_id:"id"},{$addToSet: {field:
{key2:v2, key1:v1}
}});

Mongo will see this as a duplicate and NOT this object to the array.

Post a Comment for "Can Mongodb And Its Drivers Preserve The Ordering Of Document Elements"