How to keep order in object?

560 views
Skip to first unread message

Helid2

unread,
Dec 16, 2013, 1:48:36 PM12/16/13
to mongod...@googlegroups.com
In my MongoDB document I have object like this

[_id] => MongoId Object (
    [$id] => 52a46b44aabacb5c218b4567
)
[results] => Array (
    [http://google.com] => Array (
        [position] => 1
        [data] => 42672
    )
    [http://bing.com] => Array (
        [position] => 2
        [data] => 9423
    )
    [http://yandex.com] => Array (
        [position] => 3
        [data] => 5513
    )
)

I would like to change data parameter in "bing.com" from 9423 to for instance 300. Moreover, I have to keep order of the sites. It have to looks like this

[_id] => MongoId Object (
    [$id] => 52a46b44aabacb5c218b4567
)
[results] => Array (
    [http://google.com] => Array (
        [position] => 1
        [data] => 42672
    )
    [http://bing.com] => Array (
        [position] => 2
        [data] => 300
    )
    [http://yandex.com] => Array (
        [position] => 3
        [data] => 5513
    )
)

Is this achievable in Mongo? 

Dwight Merriman

unread,
Dec 16, 2013, 4:06:31 PM12/16/13
to mongod...@googlegroups.com
i am not sure what that syntax is and such, but in general: 

- documents in mongodb collections are not ordered (much like relational tables).  so if you have documents to keep in order, you'll need to either sort when you execute the query (e.g. sort( { _id : 1 } ) or perhaps use a capped collection, as capped collections are special and order-maintaining (albeit they have certain restrictions).
- inside a BSON document, fields within documents are generally kept in order by the server, with some exceptoins.  however a driver or object  mapper might not maintain order, so it will depend on the client side tooling also
- the values within an array inside a BSON document are kept in order (arrays are arrays, not "sets")

Stephen Steneker

unread,
Dec 17, 2013, 8:11:33 PM12/17/13
to mongod...@googlegroups.com
Hi,

This question was also posted on StackOverflow and has a few answers there:

Regards,
Stephen
Reply all
Reply to author
Forward
0 new messages