jstests/core/arrayfind8.js
/**
 * Check matching and for different query types.
 * @param subQuery - part of a query, to be provided as is for a standard query and within a
 *     $elemMatch clause for a $elemMatch query
 * @param bothMatch - document matched by both standardQuery and elemMatchQuery
 * @param elemMatch - document matched by elemMatchQuery but not standardQuery
 * @param notElemMatch - document matched by standardQuery but not elemMatchQuery
 * @param additionalConstraints - additional query parameters not generated from @param subQuery
 */
Check matching and for different query types.
Check matching and for different query types .
function checkQuery(subQuery, bothMatch, elemMatch, nonElemMatch, additionalConstraints) {
    coll.drop();
    additionalConstraints = additionalConstraints || {};
    const firstSubQueryKey = Object.keySet(subQuery)[0];
    let standardQuery = null;
    if (firstSubQueryKey[0] == '$') {
        standardQuery = {$and: [{a: subQuery}, additionalConstraints]};
    } else {
        let modifiedSubQuery = {};
        modifiedSubQuery['a.' + firstSubQueryKey] = subQuery[firstSubQueryKey];
        standardQuery = {$and: [modifiedSubQuery, additionalConstraints]};
    }
    const elemMatchQuery = {$and: [{a: {$elemMatch: subQuery}}, additionalConstraints]};
    function insertValueIfNotNull(val) {
        if (val) {
            assert.commandWorked(coll.insert({a: val}));
        }
    }
    insertValueIfNotNull(bothMatch);
    insertValueIfNotNull(elemMatch);
    insertValueIfNotNull(nonElemMatch);
    checkMatch(bothMatch, elemMatch, nonElemMatch, standardQuery, elemMatchQuery, 'unindexed');
    assert.eq(coll.drop(), true);
    insertValueIfNotNull(bothMatch);
    insertValueIfNotNull(elemMatch);
    assert.commandWorked(coll.createIndex(currentIndexSpec));
    checkMatch(bothMatch, elemMatch, null, standardQuery, elemMatchQuery, 'single key index');
    insertValueIfNotNull(nonElemMatch);
    assert.commandWorked(coll.insert({a: [-1, -2]}));
    assert.commandWorked(coll.insert({a: {b: [-1, -2]}}));
    checkMatch(bothMatch, elemMatch, nonElemMatch, standardQuery, elemMatchQuery, 'multikey index');
}
function checkQuery ( subQuery , bothMatch , elemMatch , nonElemMatch , additionalConstraints ) { coll . drop ( ) ; additionalConstraints = additionalConstraints || { } ; const firstSubQueryKey = Object . keySet ( subQuery ) [ 0 ] ; let standardQuery = null ; if ( firstSubQueryKey [ 0 ] == '$' ) { standardQuery = { $and : [ { a : subQuery } , additionalConstraints ] } ; } else { let modifiedSubQuery = { } ; modifiedSubQuery [ 'a.' + firstSubQueryKey ] = subQuery [ firstSubQueryKey ] ; standardQuery = { $and : [ modifiedSubQuery , additionalConstraints ] } ; } const elemMatchQuery = { $and : [ { a : { $elemMatch : subQuery } } , additionalConstraints ] } ; function insertValueIfNotNull ( val ) { if ( val ) { assert . commandWorked ( coll . insert ( { a : val } ) ) ; } } insertValueIfNotNull ( bothMatch ) ; insertValueIfNotNull ( elemMatch ) ; insertValueIfNotNull ( nonElemMatch ) ; checkMatch ( bothMatch , elemMatch , nonElemMatch , standardQuery , elemMatchQuery , 'unindexed' ) ; assert . eq ( coll . drop ( ) , true ) ; insertValueIfNotNull ( bothMatch ) ; insertValueIfNotNull ( elemMatch ) ; assert . commandWorked ( coll . createIndex ( currentIndexSpec ) ) ; checkMatch ( bothMatch , elemMatch , null , standardQuery , elemMatchQuery , 'single key index' ) ; insertValueIfNotNull ( nonElemMatch ) ; assert . commandWorked ( coll . insert ( { a : [ - 1 , - 2 ] } ) ) ; assert . commandWorked ( coll . insert ( { a : { b : [ - 1 , - 2 ] } } ) ) ; checkMatch ( bothMatch , elemMatch , nonElemMatch , standardQuery , elemMatchQuery , 'multikey index' ) ; }
Check matching and for different query types.
Check matching and for different query types .
// Construct standard and elemMatch queries from subQuery. // If the subQuery contains a field rather than operators, append to the 'a' field. // Save all documents and check matching without indexes. // Check matching and index bounds for a single key index. // The nonElemMatch document is not tested here, as it will often make the index multikey. // Check matching and index bounds for a multikey index. // Now the nonElemMatch document is tested. // Force the index to be multikey.