Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'],  ...]

But document elements are inherently ordered within the underlying BSON data structure, so in principle:

{k1 : 'v1', 
 k2 : 'v2',  ...}

should be enough. However I expect most language bindings will interpret these as associative arrays, and thus potentially scramble the ordering. So what I need to know is:

  • Does MongoDB itself promise to preserve item ordering of the second form.
  • Do language bindings have some API which can extract it ordered form -- even if the usual "convenient" API returns an associative array.

I am mostly interested in Javascript and PHP here, but I would also like to know about other languages. Any help is appreciated, or just a link to some documentation where I can go RTM.

like image 725
Adrian Ratnapala Avatar asked Dec 20 '11 14:12

Adrian Ratnapala


People also ask

Does MongoDB return documents in order?

MongoDB does not store documents in a collection in a particular order. When sorting on a field which contains duplicate values, documents containing those values may be returned in any order.

How does MongoDB store documents?

Documents are stored on disk using block compression to reduce storage usage. Documents are automatically uncompressed in memory when retrieved by the MongoDB server. Each collection & index is stored in a separate file within the storage.

What is the limitation of a document in MongoDB?

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

Which of the following is correct about MongoDB?

Which of the following is correct option ? MongoDB is a NoSQL database is correct option.


2 Answers

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.

like image 192
mnemosyn Avatar answered Sep 30 '22 02:09

mnemosyn


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

like image 43
mattwad Avatar answered Sep 30 '22 03:09

mattwad