在C ++中正确修改mongo :: BSONObj

时间:2014-03-14 07:22:54

标签: c++ mongodb mongo-cxx-driver

我编写了一个使用mongodb进行数据存储的软件。查询返回数据后,BSONObj用于很多不同的地方。

目前我需要增加查询返回的BSONObj的C ++端修改的可能性。由于后面的部分非常大,我只能修改查询部分,但是看看BSONObj和BSONElement引用我看不到编辑BSONObj的正确方法而不在每次编辑时重建它。

修改代码与此类似:

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? 
        }

    }
}     

我研究的其中一个选项是创建单个BSONObjBuilder并修改它,但它没有提供查询对象的选项,文档也没有说明现有字段和append()

1 个答案:

答案 0 :(得分:0)

我认为你想用你的想法做鬼鬼祟祟的事情。但遗憾的是,如果我没记错,你就无法修改完成的BSON对象。完成我的意思是完成。

顺便说一下,这不是一个问题。

如果要过滤查询结果,请使用MongoDB聚合框架。就像你只需要来自大量文档的用户名一样。

这对你有帮助。

client commands reference (ver: 2.3.2)

example of client commands