jstests/sharding/query/merge_to_non_existing.js
/**
 * Run an aggregation on 'sourceColl' that writes documents to 'targetColl' with $merge.
 */
Run an aggregation on 'sourceColl' that writes documents to 'targetColl' with $merge.
Run an aggregation on ' sourceColl ' that writes documents to ' targetColl ' with $merge .
function testMerge(sourceColl, targetColl, shardedSource) {
    sourceColl.drop();
    if (shardedSource) {
        st.shardColl(sourceColl, {_id: 1}, {_id: 0}, {_id: 1}, sourceDB.getName());
    }
    for (let i = 0; i < 10; i++) {
        assert.commandWorked(sourceColl.insert({_id: i}));
    }
    withEachMergeMode(({whenMatchedMode, whenNotMatchedMode}) => {
        if (whenMatchedMode == "fail" || whenNotMatchedMode == "fail")
            return;
        targetColl.drop();
        sourceColl.aggregate([{
            $merge: {
                into: {db: targetColl.getDB().getName(), coll: targetColl.getName()},
                whenMatched: whenMatchedMode,
                whenNotMatched: whenNotMatchedMode,
                on: "_id"
            }
        }]);
        assert.eq(whenNotMatchedMode == "discard" ? 0 : 10, targetColl.find().itcount());
    });
    withEachMergeMode(({whenMatchedMode, whenNotMatchedMode}) => {
        if (whenMatchedMode == "fail" || whenNotMatchedMode == "fail")
            return;
        targetColl.drop();
        assertErrorCode(
            sourceColl,
            [{
                $merge: {
                    into: {db: targetColl.getDB().getName(), coll: targetColl.getName()},
                    whenMatched: whenMatchedMode,
                    whenNotMatched: whenNotMatchedMode,
                    on: "not_allowed"
                }
            }],
            51190);
    });
    if (targetColl.getDB() == sourceColl.getDB()) {
        jsTestLog(`Testing $out from ${sourceColl.getFullName()} ` +
                  `(${shardedSource ? "sharded" : "unsharded"}) to ${targetColl.getFullName()} ` +
                  `with legacy syntax`);
        targetColl.drop();
        sourceColl.aggregate([{$out: targetColl.getName()}]);
        assert.eq(10, targetColl.find().itcount());
    }
}
function testMerge ( sourceColl , targetColl , shardedSource ) { sourceColl . drop ( ) ; if ( shardedSource ) { st . shardColl ( sourceColl , { _id : 1 } , { _id : 0 } , { _id : 1 } , sourceDB . getName ( ) ) ; } for ( let i = 0 ; i < 10 ; i ++ ) { assert . commandWorked ( sourceColl . insert ( { _id : i } ) ) ; } withEachMergeMode ( ( { whenMatchedMode , whenNotMatchedMode } ) => { if ( whenMatchedMode == "fail" || whenNotMatchedMode == "fail" ) return ; targetColl . drop ( ) ; sourceColl . aggregate ( [ { $merge : { into : { db : targetColl . getDB ( ) . getName ( ) , coll : targetColl . getName ( ) } , whenMatched : whenMatchedMode , whenNotMatched : whenNotMatchedMode , on : "_id" } } ] ) ; assert . eq ( whenNotMatchedMode == "discard" ? 0 : 10 , targetColl . find ( ) . itcount ( ) ) ; } ) ; withEachMergeMode ( ( { whenMatchedMode , whenNotMatchedMode } ) => { if ( whenMatchedMode == "fail" || whenNotMatchedMode == "fail" ) return ; targetColl . drop ( ) ; assertErrorCode ( sourceColl , [ { $merge : { into : { db : targetColl . getDB ( ) . getName ( ) , coll : targetColl . getName ( ) } , whenMatched : whenMatchedMode , whenNotMatched : whenNotMatchedMode , on : "not_allowed" } } ] , 51190 ) ; } ) ; if ( targetColl . getDB ( ) == sourceColl . getDB ( ) ) { jsTestLog ( ` ${ sourceColl . getFullName ( ) } ` + ` ${ shardedSource ? "sharded" : "unsharded" } ${ targetColl . getFullName ( ) } ` + ` ` ) ; targetColl . drop ( ) ; sourceColl . aggregate ( [ { $out : targetColl . getName ( ) } ] ) ; assert . eq ( 10 , targetColl . find ( ) . itcount ( ) ) ; } }
Run an aggregation on 'sourceColl' that writes documents to 'targetColl' with $merge.
Run an aggregation on ' sourceColl ' that writes documents to ' targetColl ' with $merge .
// Test the behavior for each of the $merge modes. Since the target collection does not // exist, the behavior should be identical. // Skip the combination of merge modes which will fail depending on the contents of the // source and target collection, as this will cause the assertion below to trip. // Test that $merge fails if the "on" field is anything but "_id" when the target collection // does not exist. // Skip the combination of merge modes which will fail depending on the contents of the // source and target collection, as this will cause the assertion below to trip. // If 'targetColl' is in the same database as 'sourceColl', test that the legacy $out works // correctly.