[SERVER-2183] Using C++ and BSON, how do I serialize a key/value style container like map or unordered_map Created: 06/Dec/10  Updated: 12/Jul/16  Resolved: 16/Jan/11

Status: Closed
Project: Core Server
Component/s: Internal Client
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: MediaMath Mongo Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

There are overloaded << operators for serializing an std::vector and std::list into a BSONObjBuilder but none for containers that have key value pairs.

I understand that all mongo arrays are really key/value pairs that simply default to "0", "1", "2", etc for the keys if you treat them as simple arrays.

Could you provide an example of how I might serialize an STL map into a BSONObjBuilder?



 Comments   
Comment by whoward [ 08/Apr/11 ]

Doh!

Thanks.

Walt


GoogleTalk: walterhoward@gmail.com
Phone: (646) 652-8654

Comment by Mathias Stearn [ 06/Dec/10 ]

BSONObjBuilder b
for (map<string, SomeType>::const_iterator it(some_map.begin()), end(some_map.end()); it != end; ++it)
b.append(it->first, it->second);

BSONObj obj = b.obj()

Generated at Thu Feb 08 02:59:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.