How to store an associative array in mongodb

944 views
Skip to first unread message

Andrew Pearce

unread,
Nov 22, 2011, 10:47:23 AM11/22/11
to mongodb-user
My application uses an stl::map<int, string> to store data arriving
tick by tick. I want to store this in mongodb and have the database
insert new entries into the map, and update the value for items in the
map that have changed. Basically, I want the normal map::insert
semantics from the database.

For example ...

update 1 contains:
1 One
2 Two
5 Five

After the update, I want the database to contain
1 One
2 Two
5 Five

update 2 contains:
3 Three
4 Four

After the update, I want the database to contain
1 One
2 Two
3 Three
4 Four
5 Five

update 3 contains
2 New Two
4 New Four


After the update, I want the database to contain
1 One
2 New Two
3 Three
4 New Four
5 Five


Can someone please suggest an optimal way to code this? I'm new to
mongodb and finding the description of the modifier, upsert API a bit
hard to unravel. I can see ways to do it but which way is best.

Thanks

Eliot Horowitz

unread,
Nov 22, 2011, 2:15:54 PM11/22/11
to mongod...@googlegroups.com
Would be something like:

db.foo.update( { _id : ... } , { $inc : { "map.0" : 2 , "map.5" : 54 }
} , true )

> --
> 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.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages