jstests/core/update_with_pipeline.js
/**
 * Confirms that an update returns the expected set of documents. 'nModified' documents from
 * 'resultDocList' must match. 'nModified' may be smaller then the number of elements in
 * 'resultDocList'. This allows for the case where there are multiple documents that could be
 * updated, but only one is actually updated due to a 'multi: false' argument. Constant values
 * to the update command are passed in the 'constants' argument.
 */
Confirms that an update returns the expected set of documents. 'nModified' documents from
'resultDocList' must match. 'nModified' may be smaller then the number of elements in
'resultDocList'. This allows for the case where there are multiple documents that could be
updated, but only one is actually updated due to a 'multi: false' argument. Constant values
to the update command are passed in the 'constants' argument.
Confirms that an update returns the expected set of documents . ' nModified ' documents from ' resultDocList ' must match . ' nModified ' may be smaller then the number of elements in ' resultDocList ' . This allows for the case where there are multiple documents that could be updated but only one is actually updated due to a ' multi : false ' argument . Constant values to the update command are passed in the ' constants ' argument .
function testUpdate({
    query,
    initialDocumentList,
    update,
    resultDocList,
    nModified,
    options = {},
    constants = undefined
}) {
    assert.eq(initialDocumentList.length, resultDocList.length);
    assert.commandWorked(coll.remove({}));
    assert.commandWorked(coll.insert(initialDocumentList));
    const upd = Object.assign({q: query, u: update}, options);
    if (constants !== undefined) {
        upd.c = constants;
    }
    const res = assert.commandWorked(db.runCommand({update: collName, updates: [upd]}));
    assert.eq(nModified, res.nModified);
    let nMatched = 0;
    for (let i = 0; i < resultDocList.length; ++i) {
        if (0 === bsonWoCompare(coll.findOne(resultDocList[i]), resultDocList[i])) {
            ++nMatched;
        }
    }
    assert.eq(nModified, nMatched, `actual=${coll.find().toArray()}, expected=${resultDocList}`);
}
function testUpdate ( { query , initialDocumentList , update , resultDocList , nModified , options = { } , constants = undefined } ) { assert . eq ( initialDocumentList . length , resultDocList . length ) ; assert . commandWorked ( coll . remove ( { } ) ) ; assert . commandWorked ( coll . insert ( initialDocumentList ) ) ; const upd = Object . assign ( { q : query , u : update } , options ) ; if ( constants !== undefined ) { upd . c = constants ; } const res = assert . commandWorked ( db . runCommand ( { update : collName , updates : [ upd ] } ) ) ; assert . eq ( nModified , res . nModified ) ; let nMatched = 0 ; for ( let i = 0 ; i < resultDocList . length ; ++ i ) { if ( 0 === bsonWoCompare ( coll . findOne ( resultDocList [ i ] ) , resultDocList [ i ] ) ) { ++ nMatched ; } } assert . eq ( nModified , nMatched , ` ${ coll . find ( ) . toArray ( ) } ${ resultDocList } ` ) ; }
Confirms that an update returns the expected set of documents.
Confirms that an update returns the expected set of documents .