Hi, 
I would like to do something like this
std::map<int, mongo::BSONObjBuilder> items
for (i = 0; i<= 10;i++) { items[i].append("description","Car").append("type","vehicle").append("color","green"); }
I tried this but it generates compile time errors. Is this possible? If not what would be the best way to implement something like this?
Signalfrax's gravatar image asked Apr 9 2012 at 10:38 in Mongodb-User by Signalfrax

3 Answers

The issue is that std::map requires that values are copyable, but BSONObjBuilder is explicity defined as non-copyable (https://github.com/mongodb/mongo/blob/r2.0.4/bson/bsonobjbuilder.h#L86). What are you trying to achieve with this?
Mathias Stearn's gravatar image answered Apr 9 2012 at 11:21 by Mathias Stearn
Basically I have a map that stores POST data that I receive from a form The plain POST data looks like this:
items[0]["type"] = Car& items[0]["description"] = Mazarati& items[1]"type"] = Plane& items[1]["description"] = Boeing ...
At the server side this data is stored in a map  i.e. std::map <std::string, std::string> postdata;
postdata looks like this
postdata["items[0]["type"]"] = "Car" postdata["items[0]["description"]"] = "Mazarati" postdata["items[1]"type"]"] = "Plane" postdata["items[1]["description"]"] = "Boeing"
And I'm passing this map to a function to be validated and eventually store them in a mongodb document that looks like this:
some document ... {    items: [{"type":"Car","description":"Mazarati"}, {"type":"Plane","description":"Boeing"},...] }
For this I need to iterate through the postdata map and store the data in it's corresponding BSONObjBuilder;
And it's this last point that I'am struggling with.
Signalfrax's gravatar image answered Apr 10 2012 at 06:44 by Signalfrax
I would suggest building directly into the output array:
    BSONObjBuilder topLevel;     {         BSONArrayBuilder items (topLevel.subarrayStart("items"));         {             BSONObjBuilder item (items.subobjStart());             item.append("type", "Car");             item.append("description", "Mazarati");             item.done();         }         items.done();     }     BSONObj finished (topLevel.obj());
The {} are optional but make the scopes clear. You will probably want to replace them with loops in the real code. This style has the advantage that even though you are using multiple Builders, they all share the same underlying BufBuilder which means that the data isn't copied multiple times.
Mathias Stearn's gravatar image answered Apr 10 2012 at 08:08 by Mathias Stearn