Preserving field order

719 views
Skip to first unread message

Christopher Gokey

unread,
Dec 5, 2013, 8:55:20 AM12/5/13
to mongod...@googlegroups.com
I noticed that when inserting a DBObject into the database the field order is preserved UNTIL the DBObject is updated, then all goes to haywire and all the fields in the DBObject are shuffled.   Is there ...anyway... to preserve the order of the fields in the DBObject?   It would make life so much more simpler for something I'm writing... :)

Thanks,
Chris

Stephen Steneker

unread,
Dec 18, 2013, 7:35:01 PM12/18/13
to mongod...@googlegroups.com
On Friday, December 6, 2013 12:55:20 AM UTC+11, Christopher Gokey wrote:
I noticed that when inserting a DBObject into the database the field order is preserved UNTIL the DBObject is updated, then all goes to haywire and all the fields in the DBObject are shuffled.   Is there ...anyway... to preserve the order of the fields in the DBObject?   It would make life so much more simpler for something I'm writing... :)

Hi Chris,

Field reordering on update is a known server issue which has been fixed in the 2.5 development branch of MongoDB (as at 2.5.2)


As a workaround you could potentially use an array to preserve order for a set of fields, but that may not be that much simpler for your use case.

It's worth noting that the default document representation for most MongoDB drivers uses a hash or dictionary (typically not order preserving), so assumptions on field order may lead to unexpected bugs :).

Regards,
Stephen

Christopher Gokey

unread,
Dec 18, 2013, 10:43:30 PM12/18/13
to mongod...@googlegroups.com
Thanks Steven, I'll give 2.5.2 branch a whirl with my code and see if I see any "unexpected results."   I'm really excited if this works, as I'm translating XML documents using a SAXParser on the fly to DBObjects and when I pull things back out and convert from DBObjects to XML, the field order has to be correct otherwise, I get xml schema errors.   Right now, my work around is to reorder the fields, but obviously that isn't ideal.   So this fix will help a lot.

Chris


--
--
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
 
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/5JPF33o9JZM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Christopher D. Gokey
NASA Global Change Master Directory
http://gcmd.gsfc.nasa.gov/
Wyle Information Systems, Inc.
(301) 358-3628        FAX: (702) 993-4573

Asya Kamsky

unread,
Dec 25, 2013, 10:49:46 PM12/25/13
to mongodb-user
Make sure you use the latest 2.5 (which is currently 2.5.4 soon to be
2.5.5) so that you have the latest development branch, and remember
those are not for production use but testing only.

Asya
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Christopher Gokey

unread,
Mar 11, 2014, 11:56:34 PM3/11/14
to mongod...@googlegroups.com
I finally was able to get back to this and so far 2.6 nightly build, all seems to be working well.

So right now, I've got some code that will take an arbitrary XML document and turn it into a DBObject.
The order of fields now seems to be preserved even if an update operation is performed.

For example, I'm doing something like this:

{
    public static void main(String argv[]) throws ParserConfigurationException, TransformerException, SAXException, IOException {
        Document document = DomUtilities.getDocument(getTestXml());
        NodeList list = document.getElementsByTagName("book");
        MongoClient client = new MongoClient("localhost", 27018);  // Mongo 2.6 is 27018
        DB db = client.getDB("example");
        DBCollection collection = db.getCollection("books");

        // Insert all books
        for (int i=0; i<list.getLength(); i++) {
            Node node = list.item(i);
            DBObject object = DBObjectBuilder.createBasicDBObject(node);
            collection.insert(object);
        }

        // Now just search for genre Computer.
        DBObject query = new BasicDBObject();
        query.put("book.genre", "Computer");
        DBCursor cursor = collection.find(query);
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
        cursor.close();
    }
}

See books.xml attached.   The DBObject's representation looks like this (and the result of my run):

{
{ "_id" : { "$oid" : "531fd4be30047e316b0d7de3"} , "book" : [ { "_id" : "bk101" , "author" : [ "Gambardella, Matthew"] , "title" : [ "XML Developer's Guide"] , "genre" : [ "Computer"] , "price" : [ "44.95"] , "publish_date" : [ "2000-10-01"] , "description" : [ "An in-depth look at creating applications\n      with XML."]}]}
{ "_id" : { "$oid" : "531fd4bf30047e316b0d7dec"} , "book" : [ { "_id" : "bk110" , "author" : [ "O'Brien, Tim"] , "title" : [ "Microsoft .NET: The Programming Bible"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-09"] , "description" : [ "Microsoft's .NET initiative is explored in\n      detail in this deep programmer's reference."]}]}
{ "_id" : { "$oid" : "531fd4bf30047e316b0d7ded"} , "book" : [ { "_id" : "bk111" , "author" : [ "O'Brien, Tim"] , "title" : [ "MSXML3: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-01"] , "description" : [ "The Microsoft MSXML3 parser is covered in\n      detail, with attention to XML DOM interfaces, XSLT processing,\n      SAX and more."]}]}
{ "_id" : { "$oid" : "531fd4bf30047e316b0d7dee"} , "book" : [ { "_id" : "bk112" , "author" : [ "Galos, Mike"] , "title" : [ "Visual Studio 7: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "49.95"] , "publish_date" : [ "2001-04-16"] , "description" : [ "Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are\n      integrated into a comprehensive development\n      environment."]}]}
{ "_id" : { "$oid" : "531fd4e93004a1b0b6b4f3de"} , "book" : [ { "_id" : "bk101" , "author" : [ "Gambardella, Matthew"] , "title" : [ "XML Developer's Guide"] , "genre" : [ "Computer"] , "price" : [ "44.95"] , "publish_date" : [ "2000-10-01"] , "description" : [ "An in-depth look at creating applications\n      with XML."]}]}
{ "_id" : { "$oid" : "531fd4e93004a1b0b6b4f3e7"} , "book" : [ { "_id" : "bk110" , "author" : [ "O'Brien, Tim"] , "title" : [ "Microsoft .NET: The Programming Bible"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-09"] , "description" : [ "Microsoft's .NET initiative is explored in\n      detail in this deep programmer's reference."]}]}
{ "_id" : { "$oid" : "531fd4e93004a1b0b6b4f3e8"} , "book" : [ { "_id" : "bk111" , "author" : [ "O'Brien, Tim"] , "title" : [ "MSXML3: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-01"] , "description" : [ "The Microsoft MSXML3 parser is covered in\n      detail, with attention to XML DOM interfaces, XSLT processing,\n      SAX and more."]}]}
{ "_id" : { "$oid" : "531fd4e93004a1b0b6b4f3e9"} , "book" : [ { "_id" : "bk112" , "author" : [ "Galos, Mike"] , "title" : [ "Visual Studio 7: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "49.95"] , "publish_date" : [ "2001-04-16"] , "description" : [ "Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are\n      integrated into a comprehensive development\n      environment."]}]}
{ "_id" : { "$oid" : "531fd5d030045b3db32abc54"} , "book" : [ { "_id" : "bk101" , "author" : [ "Gambardella, Matthew"] , "title" : [ "XML Developer's Guide"] , "genre" : [ "Computer"] , "price" : [ "44.95"] , "publish_date" : [ "2000-10-01"] , "description" : [ "An in-depth look at creating applications\n      with XML."]}]}
{ "_id" : { "$oid" : "531fd5d030045b3db32abc5d"} , "book" : [ { "_id" : "bk110" , "author" : [ "O'Brien, Tim"] , "title" : [ "Microsoft .NET: The Programming Bible"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-09"] , "description" : [ "Microsoft's .NET initiative is explored in\n      detail in this deep programmer's reference."]}]}
{ "_id" : { "$oid" : "531fd5d030045b3db32abc5e"} , "book" : [ { "_id" : "bk111" , "author" : [ "O'Brien, Tim"] , "title" : [ "MSXML3: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-01"] , "description" : [ "The Microsoft MSXML3 parser is covered in\n      detail, with attention to XML DOM interfaces, XSLT processing,\n      SAX and more."]}]}
{ "_id" : { "$oid" : "531fd5d030045b3db32abc5f"} , "book" : [ { "_id" : "bk112" , "author" : [ "Galos, Mike"] , "title" : [ "Visual Studio 7: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "49.95"] , "publish_date" : [ "2001-04-16"] , "description" : [ "Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are\n      integrated into a comprehensive development\n      environment."]}]}
{ "_id" : { "$oid" : "531fd62f30043ce65b96eccc"} , "book" : [ { "_id" : "bk101" , "author" : [ "Gambardella, Matthew"] , "title" : [ "XML Developer's Guide"] , "genre" : [ "Computer"] , "price" : [ "44.95"] , "publish_date" : [ "2000-10-01"] , "description" : [ "An in-depth look at creating applications\n      with XML."]}]}
{ "_id" : { "$oid" : "531fd62f30043ce65b96ecd5"} , "book" : [ { "_id" : "bk110" , "author" : [ "O'Brien, Tim"] , "title" : [ "Microsoft .NET: The Programming Bible"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-09"] , "description" : [ "Microsoft's .NET initiative is explored in\n      detail in this deep programmer's reference."]}]}
{ "_id" : { "$oid" : "531fd62f30043ce65b96ecd6"} , "book" : [ { "_id" : "bk111" , "author" : [ "O'Brien, Tim"] , "title" : [ "MSXML3: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-01"] , "description" : [ "The Microsoft MSXML3 parser is covered in\n      detail, with attention to XML DOM interfaces, XSLT processing,\n      SAX and more."]}]}
{ "_id" : { "$oid" : "531fd62f30043ce65b96ecd7"} , "book" : [ { "_id" : "bk112" , "author" : [ "Galos, Mike"] , "title" : [ "Visual Studio 7: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "49.95"] , "publish_date" : [ "2001-04-16"] , "description" : [ "Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are\n      integrated into a comprehensive development\n      environment."]}]}
{ "_id" : { "$oid" : "531fda11300481c510c4fc02"} , "book" : [ { "_id" : "bk101" , "author" : [ "Gambardella, Matthew"] , "title" : [ "XML Developer's Guide"] , "genre" : [ "Computer"] , "price" : [ "44.95"] , "publish_date" : [ "2000-10-01"] , "description" : [ "An in-depth look at creating applications\n      with XML."]}]}
{ "_id" : { "$oid" : "531fda11300481c510c4fc0b"} , "book" : [ { "_id" : "bk110" , "author" : [ "O'Brien, Tim"] , "title" : [ "Microsoft .NET: The Programming Bible"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-09"] , "description" : [ "Microsoft's .NET initiative is explored in\n      detail in this deep programmer's reference."]}]}
{ "_id" : { "$oid" : "531fda11300481c510c4fc0c"} , "book" : [ { "_id" : "bk111" , "author" : [ "O'Brien, Tim"] , "title" : [ "MSXML3: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "36.95"] , "publish_date" : [ "2000-12-01"] , "description" : [ "The Microsoft MSXML3 parser is covered in\n      detail, with attention to XML DOM interfaces, XSLT processing,\n      SAX and more."]}]}
{ "_id" : { "$oid" : "531fda11300481c510c4fc0d"} , "book" : [ { "_id" : "bk112" , "author" : [ "Galos, Mike"] , "title" : [ "Visual Studio 7: A Comprehensive Guide"] , "genre" : [ "Computer"] , "price" : [ "49.95"] , "publish_date" : [ "2001-04-16"] , "description" : [ "Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are\n      integrated into a comprehensive development\n      environment."]}]}
}

What kind of issues (i.e., limitations) will I run into with mongo by representating  all values as an array?
I noticed one limitation is that I couldn't do a multi key sort.   Any other glaring holes with this kind of implementation/representation of XML data?

Thanks,
Chris
Books.xml

Christopher Gokey

unread,
Mar 12, 2014, 8:54:18 AM3/12/14
to mongod...@googlegroups.com
Stephen,

Just an update... A quick look under the hood, the Java driver seems to implement this as a LinkHashMap, which does preserve field order.   So far my tests look good, on the Java side.

Chris

Stephen Steneker

unread,
Mar 13, 2014, 8:07:22 AM3/13/14
to mongod...@googlegroups.com
On Wednesday, 12 March 2014 23:54:18 UTC+11, Christopher Gokey wrote:
Just an update... A quick look under the hood, the Java driver seems to implement this as a LinkHashMap, which does preserve field order.   So far my tests look good, on the Java side.

Hi Chris,

Thanks for the update! If you do spot any issues, please let us know the specific Java driver and MongoDB server versions being used.

Regards,
Stephen

Reply all
Reply to author
Forward
0 new messages