本文整理汇总了C++中BSONObj::getBSONArray方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::getBSONArray方法的具体用法?C++ BSONObj::getBSONArray怎么用?C++ BSONObj::getBSONArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObj
的用法示例。
在下文中一共展示了BSONObj::getBSONArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testCopyBSON
void testCopyBSON()
{
cout << "testCopyBSON" << endl;
BSONObj* objOrig = new BSONObj();
// Add in
objOrig->add("int", 1);
objOrig->add("string", (char*)"test");
objOrig->add("long", (__int64)1L);
objOrig->add("double", 1.1);
BSONObj rel;
rel.add("innertext", (char*)"inner text");
objOrig->add("rel1", rel);
BSONArrayObj array;
BSONObj b1;
b1.add("b1", "test");
array.add(b1);
BSONObj b2;
b2.add("b1", "test2");
array.add(b2);
objOrig->add("array", array);
BSONObj* obj = new BSONObj(*objOrig);
delete objOrig;
objOrig = NULL;
TEST_ASSERT(obj->has("int"));
TEST_ASSERT(obj->getInt("int") == 1);
TEST_ASSERT(strcmp(obj->getString("string"), "test") == 0);
TEST_ASSERT(obj->has("long"));
TEST_ASSERT(obj->getLong("long") == 1L);
TEST_ASSERT(obj->has("double"));
TEST_ASSERT(obj->getDouble("double") == 1.1);
BSONObj* temp = obj->getBSON("rel1");
TEST_ASSERT(temp != NULL);
TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0);
TEST_ASSERT(obj->getBSONArray("array") != NULL);
BSONArrayObj* arrayR = obj->getBSONArray("array");
TEST_ASSERT(arrayR != NULL);
TEST_ASSERT(arrayR->length() == 2);
BSONObj* el1 = arrayR->get(0);
TEST_ASSERT(el1 != NULL);
BSONObj* el2 = arrayR->get(1);
TEST_ASSERT(el2 != NULL);
delete obj;
}
示例2: testParserCollection
void testParserCollection()
{
cout << "testParserCollection" << endl;
BSONObj* obj = BSONParser::parse("{age: 1, name: 'John', salary: 3500.25, rel1: [{innertext: 'inner text'}, {innertext: 'inner text'}, {innertext: 'inner text'}, {innertext: 'inner text'} ] }");
TEST_ASSERT(obj->has("age"));
TEST_ASSERT(obj->getInt("age") == 1);
TEST_ASSERT(obj->has("name"));
TEST_ASSERT(strcmp(obj->getString("name"), "John") == 0);
TEST_ASSERT(obj->has("salary"));
TEST_ASSERT(obj->getDouble("salary") == 3500.25);
TEST_ASSERT(obj->getBSONArray("rel1") != NULL);
TEST_ASSERT(obj->getBSONArray("rel1")->length() == 4);
TEST_ASSERT(obj->getBSONArray("rel1")->get(0)->has("innertext"));
TEST_ASSERT(strcmp(obj->getBSONArray("rel1")->get(0)->getString("innertext"), "inner text") == 0);
delete obj;
}
示例3: testBSON
void testBSON()
{
cout << "testBSON" << endl;
BSONObj* obj = new BSONObj();
// Add in
obj->add("int", 1);
obj->add("string", (const char*)"test");
obj->add("long", (__int64) 10000000000L);
obj->add("double", 1.1);
BSONObj rel;
rel.add("innertext", (char*)"inner text");
obj->add("rel1", rel);
BSONArrayObj array;
BSONObj b1;
b1.add("b1", "test");
array.add(b1);
BSONObj b2;
b2.add("b1", "test2");
array.add(b2);
obj->add("array", array);
TEST_ASSERT(obj->has("int"));
TEST_ASSERT(obj->getInt("int") == 1);
TEST_ASSERT(strcmp(obj->getString("string"), "test") == 0);
TEST_ASSERT(obj->has("long"));
cout << "long: " << obj->getLong("long") << endl;
TEST_ASSERT(obj->getLong("long") == 10000000000L);
TEST_ASSERT(obj->has("double"));
TEST_ASSERT(obj->getDouble("double") == 1.1);
TEST_ASSERT(obj->has("rel1"));
TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0);
TEST_ASSERT(obj->has("array"));
BSONArrayObj* arrayR = obj->getBSONArray("array");
TEST_ASSERT(arrayR != NULL);
TEST_ASSERT(arrayR->length() == 2);
BSONObj* el1 = arrayR->get(0);
TEST_ASSERT(el1 != NULL);
BSONObj* el2 = arrayR->get(1);
TEST_ASSERT(el2 != NULL);
// test a non existant attribute
try {
obj->getLong("xx");
TEST_FAIL("The getLong should throw an exception");
} catch (BSONException e) {
}
try {
obj->getString("xxx");
TEST_FAIL("The getString should throw an exception");
} catch (BSONException e) {
}
delete obj;
}