jstests/sharding/change_streams_delete_in_txn_produces_correct_doc_key.js
/**
 * Test function which is used to test three types of delete-related commands, deleteOne(),
 * deleteMany() and findAndModify(). Ensure only documentKey instead of a full document will be
 * logged on entries for deletes in multi-document transactions, and also ensure that we can resume
 * the change stream from these delete events.
 */
Test function which is used to test three types of delete-related commands, deleteOne(),
deleteMany() and findAndModify(). Ensure only documentKey instead of a full document will be
logged on entries for deletes in multi-document transactions, and also ensure that we can resume
the change stream from these delete events.
Test function which is used to test three types of delete - related commands deleteOne () deleteMany () and findAndModify () . Ensure only documentKey instead of a full document will be logged on entries for deletes in multi - document transactions and also ensure that we can resume the change stream from these delete events .
function testDeleteInMultiDocTxn({collName, deleteCommand, expectedChanges}) {
    const coll = assertDropAndRecreateCollection(db, collName);
    assert.commandWorked(db.adminCommand({enableSharding: dbName}));
    assert.commandWorked(db.adminCommand({shardCollection: ns, key: {a: 1, _id: 1}}));
    assert.commandWorked(coll.insertMany([
        {_id: 1, a: 0, fullDoc: "It's a full document!"},
        {_id: 2, a: 0},
        {_id: 3, a: 0},
        {_id: 4, a: 1}
    ]));
    const cst = new ChangeStreamTest(db);
    let cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: coll});
    const session = db.getMongo().startSession();
    const sessionDb = session.getDatabase(db.getName());
    const sessionColl = sessionDb[collName];
    session.startTransaction();
    deleteCommand(sessionColl);
    assert.commandWorked(session.commitTransaction_forTesting());
    const changes = cst.assertNextChangesEqual({cursor: cursor, expectedChanges: expectedChanges});
    cursor = cst.startWatchingChanges({
        pipeline: [{$changeStream: {resumeAfter: changes[changes.length - 1]._id}}],
        collection: coll
    });
    assert.commandWorked(coll.insert({a: 1, _id: 5}));
    assert.docEq(cst.getOneChange(cursor).documentKey, {a: 1, _id: 5});
    cst.cleanUp();
}
function testDeleteInMultiDocTxn ( { collName , deleteCommand , expectedChanges } ) { const coll = assertDropAndRecreateCollection ( db , collName ) ; assert . commandWorked ( db . adminCommand ( { enableSharding : dbName } ) ) ; assert . commandWorked ( db . adminCommand ( { shardCollection : ns , key : { a : 1 , _id : 1 } } ) ) ; assert . commandWorked ( coll . insertMany ( [ { _id : 1 , a : 0 , fullDoc : "It's a full document!" } , { _id : 2 , a : 0 } , { _id : 3 , a : 0 } , { _id : 4 , a : 1 } ] ) ) ; const cst = new ChangeStreamTest ( db ) ; let cursor = cst . startWatchingChanges ( { pipeline : [ { $changeStream : { } } ] , collection : coll } ) ; const session = db . getMongo ( ) . startSession ( ) ; const sessionDb = session . getDatabase ( db . getName ( ) ) ; const sessionColl = sessionDb [ collName ] ; session . startTransaction ( ) ; deleteCommand ( sessionColl ) ; assert . commandWorked ( session . commitTransaction_forTesting ( ) ) ; const changes = cst . assertNextChangesEqual ( { cursor : cursor , expectedChanges : expectedChanges } ) ; cursor = cst . startWatchingChanges ( { pipeline : [ { $changeStream : { resumeAfter : changes [ changes . length - 1 ] . _id } } ] , collection : coll } ) ; assert . commandWorked ( coll . insert ( { a : 1 , _id : 5 } ) ) ; assert . docEq ( cst . getOneChange ( cursor ) . documentKey , { a : 1 , _id : 5 } ) ; cst . cleanUp ( ) ; }
Test function which is used to test three types of delete-related commands, deleteOne(),
 deleteMany() and findAndModify().
Test function which is used to test three types of delete - related commands deleteOne () deleteMany () and findAndModify () .
// Initialize the collection. // Enable sharding on DB. // Shard the collection using field 'a' as the shard key. // Open a change stream on the test collection. // Start a transaction in which to perform the delete operation(s). // Run the given 'deleteCommand' function to perform the delete(s). // Commit the transaction so that the events become visible to the change stream. // Verify that the stream returns the expected sequence of changes. // Test the change stream can be resumed after a delete event from within the transaction.