mongodb c ++ drive通过BSONObjBuilder构造一个BSONObj对象,但实际上需要将字段附加到BSONObj,如:
BSONObj base_part_query()
{
return BSON(...);
}
void q1()
{
BSONObj obj = base_part_query();
obj.appendField("q1_special","v"); // mongo driver has no such api
// query by q1
}
void q2()
{
BSONObj obj = base_part_query();
obj.appendField("q2_special","v"); // mongo driver has no such api
// query by q2
}
如果我不进行base_part_query
,则qn()中将有n个BSON(...)的重复副本。那不好。
答案 0 :(得分:4)
我找到了一种方法:
BSONObj o = (mongo::BSONObjBuilder().appendElements(base_part_query())<<"q1_special"<<BSON("$lte"<<111)).obj()