TEST_F(DocumentSourceMatchTest, MultipleMatchStagesShouldCombineIntoOne) {
    auto match1 = DocumentSourceMatch::create(BSON("a" << 1), getExpCtx());
    auto match2 = DocumentSourceMatch::create(BSON("b" << 1), getExpCtx());
    auto match3 = DocumentSourceMatch::create(BSON("c" << 1), getExpCtx());

    Pipeline::SourceContainer container;

    // Check initial state
    ASSERT_BSONOBJ_EQ(match1->getQuery(), BSON("a" << 1));
    ASSERT_BSONOBJ_EQ(match2->getQuery(), BSON("b" << 1));
    ASSERT_BSONOBJ_EQ(match3->getQuery(), BSON("c" << 1));

    container.push_back(match1);
    container.push_back(match2);
    match1->optimizeAt(container.begin(), &container);

    ASSERT_EQUALS(container.size(), 1U);
    ASSERT_BSONOBJ_EQ(match1->getQuery(), fromjson("{'$and': [{a:1}, {b:1}]}"));

    container.push_back(match3);
    match1->optimizeAt(container.begin(), &container);
    ASSERT_EQUALS(container.size(), 1U);
    ASSERT_BSONOBJ_EQ(match1->getQuery(),
                      fromjson("{'$and': [{'$and': [{a:1}, {b:1}]},"
                               "{c:1}]}"));
}
Esempio n. 2
0
    void run() {
        client.insert(nss.ns(), BSON("a" << 1));
        client.insert(nss.ns(), BSON("a" << 2));
        client.insert(nss.ns(), BSON("a" << 3));
        createSource();

        Pipeline::SourceContainer container;
        container.push_back(source());
        container.push_back(mkLimit(10));
        source()->optimizeAt(container.begin(), &container);

        // initial limit becomes limit of cursor
        ASSERT_EQUALS(container.size(), 1U);
        ASSERT_EQUALS(source()->getLimit(), 10);

        container.push_back(mkLimit(2));
        source()->optimizeAt(container.begin(), &container);
        // smaller limit lowers cursor limit
        ASSERT_EQUALS(container.size(), 1U);
        ASSERT_EQUALS(source()->getLimit(), 2);

        container.push_back(mkLimit(3));
        source()->optimizeAt(container.begin(), &container);
        // higher limit doesn't effect cursor limit
        ASSERT_EQUALS(container.size(), 1U);
        ASSERT_EQUALS(source()->getLimit(), 2);

        // The cursor allows exactly 2 documents through
        ASSERT(source()->getNext().isAdvanced());
        ASSERT(source()->getNext().isAdvanced());
        ASSERT(source()->getNext().isEOF());
    }