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()
.