void testParserRelation() { cout << "testParserRelation" << endl; BSONObj* obj = BSONParser::parse("{age: 1, name: 'John', rel1: {innertext: 'inner text', salary: 150000, rent: 10000}}"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(obj->has("name")); TEST_ASSERT(strcmp(obj->getString("name"), "John") == 0); __int32 salary = *obj->getXpath("rel1.salary"); TEST_ASSERT(salary == 150000); __int32 rent = *obj->getXpath("rel1.rent"); TEST_ASSERT(rent == 10000); TEST_ASSERT(obj->getBSON("rel1") != NULL); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0); delete obj; }
BSONContent* BSONObj::getXpath(const std::string& xpath) const { __int32 posDot = xpath.find('.'); BSONContent* result = NULL; if (posDot == string::npos) { result = getContent(xpath); } else { std::string path = xpath.substr(0, posDot); result = getContent(path); if ((result != NULL) && (result->type() == BSON_TYPE)) { BSONContentBSON* bcontent = (BSONContentBSON*)result; BSONObj* inner = (BSONObj*)*bcontent; result = inner->getXpath(xpath.substr(posDot + 1)); } } if (result != NULL) { return result->clone(); } else { return NULL; } }