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

Related Discussions

  • How Do I Get Unique Result In Mongodb Using Map Reduce in Mongodb-user

  • i have the following data stored in the mongodb like this uid: 100, country:US uid:500, country:Japan Uid:500, country:Japan uid:569, country:Itly uid:700, country:US how do i write the map reduce query which will return the count of country for unique uid..i mean i need the result in this way.. US - 2(because there are two unique uid for US) Japan - 1 (bacause there is only 1 unique id...

  • Mongodb Aggreation Using Map Reduce in Mongodb-user

  • Hi All, Need your guidance in mongodb aggregation I have started exploring mongodb couple of weeks back. I have a scenario here. I have a collection which has 3 million records.I would want to perform aggregation on the aggreation based on two keys (also need to use match condition). I used aggregation framework for the same. I came to know that aggregation would fail if the document size exceeds...

  • C# - How To Identify Whether A Class Map Is Already Registered in Mongodb-user

  • Hi,     Is there any method which can tell us, whether a class map is already registered or not. e.g.  if (ClassMap not exists) then RegisterClassMap (Custom mapping) Thanks in Advance....

  • How Many Map Reduce Functions Can We Create Per Collection? in Mongodb-user

  • How many map reduce functions can we create per collection? All the examples for MR indicate that there can be only one map-reduce-finalize method per collection; what if I want to aggregate data in same collection different ways (ex: avg, weighted avg, other statistical methods). Also the signature of MR is as follows: mapReduce(mapFunction, reduceFunction, out...). Does it mean that map and reduce...

  • Modeling A Trie In MongoDB in Mongodb-user

  • Hello, Currently, in my application I have the need to perform a longest prefix matching, like the following : In my DB I have the following : Prefix : 123 Prefix : 2345 Prefix : 1234 Prefix : 6789 If my input is 123456789, the result should be 1234, since it's the longest common prefix. Currently, I operate this by loading all the info in memory and relying on tries for fast access. While this...

  • [mongodb-user] Does Mapreduce Use The Indexes If I Specify A Query? in Mongodb-user

  • Just curious if the map reduce functionality will use the indexes on the collection if I want to run a map reduce job where date is equal to '2010-05-05' if there is an index on the date column? thanks -- 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 ...

  • How To Muilt Map Reduce To A Same Collection? in Mongodb-user

  • For example: There are 4 collections: Collection users: { id:2, name: 'xue'} Collection blogs: { id:1, useid: 2, title:'mongo1'} Collection userlikes: { id: 1, useid: 2, content:'' } Collection usertags: { id: 1, userid: 2, title: 'mongodb'} How can I get the result in one collection?     How many blogs the user post?     How many user's tags?     How many the user's likes? ...

  • Map Reduce A Collection Without Using A Key in Mongodb-user

  • Hi I have members collection, each member has an embedded array of images, friends, blog posts and videos.  I want a map reduce function to summarise the counts for these sub collections across the collection.  I do not want a record for each member back from the map reduce I only want one record for the whole collection. Here's a snippet of my code: const string map = "function() {" +                                    "  ...

  • Dynamic Value As A Key in Mongodb-user

  • Hi All, In mongodb, can I have a collection field containing the map data? If you look at the following example, I have coOccurrArticles, which is a map with article id as a key and the count as a value, in the collection. { "_id" : ObjectId("5023f934da063d12dfc12fa4"), "articleId" : NumberLong(800), "coOccurrArticles" : { "100" : 1, "700" : 2, "600" : 1, "400" : 1 } } Can I get the coOccurrArticles...

  • [mongodb-user] Is There A Logging Function in Mongodb-user

  • I have searched on the website but I haven't found anything about that. I'd like to be able to do some logging within mongodb. I know mongod handle logs etc. but it would be cool if we could log javascript code for instance. It could become a great tool to debug map reduce functions or any javascript functions or even within client code we could send the log command to log something... log...

  • How Much Can I Bet On Mongodb's Map/reduce? in Mongodb-user

  • Hi, I am in the process of moving from mysql to mongodb. Now I have a decent looking design in mongodb. But my collections just keep growing every second. Hence I plan to purge my collections based on time once a day. But I have few collections which are aggregates of a main collection (kept for performance reasons). Can you tell me if this works out? I have the following collections. 1...

  • How Can I Map From Operating System Thread To Mongo ThreadId? in Mongodb-user

  • I have a mongod process which is in constant D state, causing 100% IO saturation: 3874 mongod     20   0  368G 7855M 7659M D  1.0 52.4  0:09.96 /usr/bin/mongod -f /etc/mongod.conf I've tried to see what mongo thread it is, but I'm not sure how: mongos> db.currentOp(true) (...plenty of output...) Within db.currentOp() output, I can see some thread indication:                         "...

  • Map Reduce...how To Solve A Particular Problem? in Mongodb-user

  • I admit...i'm still trying to get my head wrapped around map reduce. Can someone offer a quick map-reduce for this problem, hopefully it'll light a bulb for me. With this data, i'd like to map reduce it and find out unique visits (each record is a visit) per store for a given month Sample data    { created_at : "10-01-2010",  store_id : "12", visitor_cookie: "a" }    { created_at : "...

  • How To Bulk Insert To A Collection From Map-reduce Results Of Another in Mongodb-user

  • I have a collection: Comment:{ClientId, CompanyCode, DocumentId, Text} I want to bulk insert into a collection: CommentCounts:{ClientId, CompanyCode, DocumentId, NumComments} I'd assume this would be straight forward (as it would be in SQL). Seems I need to use a map reduce function though.  I have following map-reduce script that gathers the counts of comments per unique key. My question...

  • [mongodb-user] Getting A Cumulative Sum By Map Reduce Function in Mongodb-user

  • I am trying to get cumulative sum in regards to specific date and time, type as well as per same user. First off, here's my dataset. { "_id" : ObjectId("5885b8fb3b765e1980f1dc3e"), "Date" : "12/21/2016", "Time" : "11:30:00", "Temperature" : 70.5, "User" : "C", "ThermalDiscomfort" : -1, "Type" : "MEDIAN", "settingID" : ObjectId("58819cfaf16a780258cee2ea")} { "_id" : ObjectId...

  • How Do I Get The MongoDB Hadoop Adapter To Output JSON Keys And Values? in Mongodb-user

  • How do I get the MongoDB Hadoop Adapter to write JSON keys and values? All the examples just write simple types (e.g. string and integers). We're wanting to migrate some map reduce jobs that have composite keys. Thank you....

  • Question On Querying And Indexing A Map Of Maps in Mongodb-user

  • Hi, We have the following collection. { "_id" : "uuid1",  "field1" : {   "ctx1" : { "ctx3" : 5, "ctx2" : 2 },     "ctx8" : { "ctx3" : 5, "ctx4" : 2 } }} { "_id" : "uuid2",  "field1" : {   "ctx2" : { "ctx3" : 5, "ctx4" : 2 },     "ctx7" : { "ctx3" : 5, "ctx8" : 2 } }} { "_id" : "uuid2",  "field1" : {   "ctx2" : { "ctx5" : 5, "ctx4" : 2 },     "ctx9" : { "ctx3" : 5, "ctx8" : 2 } }} { "...

  • Mongodb Map Reduce Jobs Getting Corrupt in Mongodb-user

  • We are using mongodb 1.6.4 in a 3 node replica setup hosted on CentOS 5.5 machines for an analytics application. We are storing docs at the rate of around 2 million a day each doc around 2 KB in size having any thing between 20 to 40 fields. We are running Map Reduce jobs triggered from PHP 5.3 code. Quite frequently we are facing the issue when Map Reduce starts failing even though insertions and...

  • Can I Map To The Auto-assigned Object ID Using C# Driver? in Mongodb-user

  • Is it possible to let MongoDB assign the Object ID from a serialized C# class but still map the assigned ID to a property? It looks like if I use the [BsonId] declaration on my "MyId" property, the driver makes the assumption that I'll also be generating the ID too.  Is that correct?  Is there any way to set it up to let MongoDB generate the id, but still access it from a deserialized object...

  • I'm New To Mongodb And I've A Basic Doubt In Mapreduce in Mongodb-user

  • suppose a collection contains {"_id": "1", "name":"prakash" } {"_id": "2", "name":"prakash" } {"_id": "3", "name":"prakash" } {"_id": "4", "name":"xyz" } I have to map the names and get result as {"_id": "3", "name":"prakash" } {"_id": "4", "name":"xyz" } All id's are unique I i've to reduce in such that a name should have only one id...