本文整理汇总了C++中BSONObj::select方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::select方法的具体用法?C++ BSONObj::select怎么用?C++ BSONObj::select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObj
的用法示例。
在下文中一共展示了BSONObj::select方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testBSONSelect
void testBSONSelect() {
cout << "\ntestBSONSelect()" << endl;
char* selectsimple;
BSONObj* obj;
BSONObj* expected;
BSONObj* result;
obj = BSONParser::parse("{ name: 'John', age: 35, one: { data: 1 }, children: [ { name: 'Joshua', age: 15}, { name: 'Mary', age: 30}] }");
selectsimple = "$\"name\", $\"one.data\"";
result = obj->select(const_cast<const char*>(selectsimple));
expected = BSONParser::parse("{ name: 'John', one: { data: 1 }}");
TEST_ASSERT(*result == *expected);
delete expected;
delete result;
selectsimple = "*";
result = obj->select(const_cast<const char*>(selectsimple));
expected = new BSONObj(*obj);
TEST_ASSERT(*result == *expected);
delete obj;
}
示例2: select
BSONObj* BSONObj::select(const char* sel) const {
std::set<std::string> columns = bson_splitSelect(sel);
bool include_all = (strcmp(sel, "*") == 0);
BSONObj* result = new BSONObj();
for (std::map<std::string, BSONContent* >::const_iterator i = this->_elements.begin(); i != this->_elements.end(); i++) {
std::string key = i->first;
if (include_all || (columns.find(key) != columns.end())) {
BSONContent* origContent = i->second;
switch (origContent->type()) {
case BSON_TYPE:
{
BSONContentBSON* bbson = (BSONContentBSON*)origContent;
BSONObj* inner = (BSONObj*)*bbson;
if (!include_all) {
char* subselect = bson_subselect(sel, key.c_str());
BSONObj* innerSubSelect = inner->select(subselect);
result->add(key, *innerSubSelect);
delete innerSubSelect;
} else {
result->add(key, *inner);
}
break;
}
case BSONARRAY_TYPE:
{
BSONContentBSONArray* bbsonarray = (BSONContentBSONArray*)origContent;
BSONArrayObj* innerArray = (BSONArrayObj*)*bbsonarray;
if (!include_all) {
char* subselect = bson_subselect(sel, key.c_str());
BSONArrayObj* innerSubArray = innerArray->select(subselect);
result->add(key, *innerSubArray);
delete innerSubArray;
} else {
result->add(key, *innerArray);
}
break;
}
case BOOL_TYPE:
{
BSONContentBoolean* bb = (BSONContentBoolean*)origContent;
bool val = *bb;
result->add(key, val);
break;
}
case INT_TYPE:
{
BSONContentInt* bint = (BSONContentInt*)origContent;
__int32 val = *bint;
result->add(key, val);
break;
}
case LONG_TYPE:
{
BSONContentLong* blong = (BSONContentLong*)origContent;
__int64 val = *blong;
result->add(key, val);
break;
}
case DOUBLE_TYPE:
{
BSONContentDouble* bdouble = (BSONContentDouble*)origContent;
double val = *bdouble;
result->add(key, val);
break;
}
case PTRCHAR_TYPE:
case STRING_TYPE:
{
BSONContentString* bstring = (BSONContentString*)origContent;
djondb::string str = *bstring;
const char* val = str.c_str();
__int32 len = str.length();
result->add(key, const_cast<char*>(val), len);
break;
}
}
}
}
return result;
}