TEST(Path, NestedNoLeaf1) { ElementPath p; ASSERT(p.init("a.b").isOK()); p.setTraverseLeafArray(false); BSONObj doc = BSON("a" << BSON_ARRAY(BSON("b" << 5) << 3 << BSONObj() << BSON("b" << BSON_ARRAY(9 << 11)) << BSON("b" << 7))); BSONElementIterator cursor(&p, doc); ASSERT(cursor.more()); BSONElementIterator::Context e = cursor.next(); ASSERT_EQUALS(5, e.element().numberInt()); ASSERT(!e.outerArray()); ASSERT(cursor.more()); e = cursor.next(); ASSERT(e.element().eoo()); ASSERT_EQUALS((string) "2", e.arrayOffset().fieldName()); ASSERT(!e.outerArray()); ASSERT(cursor.more()); e = cursor.next(); ASSERT_EQUALS(Array, e.element().type()); ASSERT_EQUALS(2, e.element().Obj().nFields()); ASSERT(e.outerArray()); ASSERT(cursor.more()); e = cursor.next(); ASSERT_EQUALS(7, e.element().numberInt()); ASSERT(!e.outerArray()); ASSERT(!cursor.more()); }
TEST(Path, RootArray2) { ElementPath p; ASSERT(p.init("a").isOK()); p.setTraverseLeafArray(false); BSONObj doc = BSON("x" << 4 << "a" << BSON_ARRAY(5 << 6)); BSONElementIterator cursor(&p, doc); ASSERT(cursor.more()); BSONElementIterator::Context e = cursor.next(); ASSERT(e.element().type() == Array); ASSERT(!cursor.more()); }
static BSONElement extractKeyElementFromMatchable(const MatchableDocument& matchable, const StringData& pathStr) { ElementPath path; path.init(pathStr); path.setTraverseNonleafArrays(false); path.setTraverseLeafArray(false); MatchableDocument::IteratorHolder matchIt(&matchable, &path); if (!matchIt->more()) return BSONElement(); BSONElement matchEl = matchIt->next().element(); // We shouldn't have more than one element - we don't expand arrays dassert(!matchIt->more()); return matchEl; }