0

I have written a software that uses mongodb for data storage. After query has returned data, the BSONObj is used in lot of different places.

At the moment I need to add a possibility for C++ side modifications of the BSONObj returned by query. As the later part is quite big, I can only modify the query part, but looking at BSONObj and BSONElement references I see no correct way of editing BSONObj without rebuilding it on each edit.

The modification code looks something similar to this:

mongo::BSONObj obj=GetQueryResults(); 
vector<mongo::BSONObj> mods=GetMods();
for(auto mod:mods){
   mod=mod.remove_field("_id");
   std::set<std::string> fields;
   mod.getFieldNames(fields);
   for(auto & field: fields){
        if(obj.hasElement(field)){
           // rebuild with field modified?
           // this includes both value replacement
           // and something like incrementing etc. 
        }else{
           // rebuild with extra field? 
        }

    }
}     

One of the options I looked into was creating a single BSONObjBuilder and modifying that, but it offers no options to query objects and documentation does not say anything about existing fields and append().

twid
  • 6,368
  • 4
  • 32
  • 50
UldisK
  • 1,619
  • 1
  • 17
  • 25

1 Answers1

0

I think you want to do something sneaky, with your idea. But sadly if I remember correctly, you cannot modify a finished BSON object. Finished I mean completed.

By the way this was not a question.

If you want to filter the result of the query use the MongoDB aggregation framework. Like you need only the name of the users from a huge collection of documents.

This will help for you.

client commands reference (ver: 2.3.2)

example of client commands