jstests/sharding/libs/update_shard_key_helpers.js
// commands that will change the shard key value and cause a document to move to a different shard.
commands that will change the shard key value and cause a document to move to a different shard.
commands that will change the shard key value and cause a document to move to a different shard .
function assertCanUpdatePrimitiveShardKeyHashedChangeShards(
    st, kDbName, ns, session, sessionDB, inTxn) {
    let docsToInsert =
        [{"x": 4, "a": 3}, {"x": 78}, {"x": 100}, {"x": 300, "a": 3}, {"x": 500, "a": 6}];
    shardCollectionMoveChunks(
        st, kDbName, ns, {"x": "hashed"}, docsToInsert, {"x": 100}, {"x": 300});
    let docsOnShard0 = st.rs0.getPrimary().getDB(kDbName).foo.find().toArray();
    let docsOnShard1 = st.rs1.getPrimary().getDB(kDbName).foo.find().toArray();
    assert.gte(docsOnShard0.length, 2);
    assert.gte(docsOnShard1.length, 2);
    st.s.getDB(kDbName).foo.remove({"x": docsOnShard0[1].x});
    st.s.getDB(kDbName).foo.remove({"x": docsOnShard1[1].x});
    let queries = [{"x": docsOnShard0[0].x}, {"x": docsOnShard1[0].x}];
    let updates = [{"$set": {"x": docsOnShard1[1].x}}, {"x": docsOnShard0[1].x}];
    let upsert = false;
    assertUpdateSucceeds(st, session, sessionDB, inTxn, queries[0], updates[0], upsert);
    assertHashedShardKeyUpdateCorrect(
        st, sessionDB, kDbName, queries[0], updates[0], upsert, false);
    assertUpdateSucceeds(st, session, sessionDB, inTxn, queries[1], updates[1], upsert);
    assertHashedShardKeyUpdateCorrect(st, sessionDB, kDbName, queries[1], updates[1], upsert, true);
    st.s.getDB(kDbName).foo.remove({"x": docsOnShard0[1].x});
    st.s.getDB(kDbName).foo.remove({"x": docsOnShard1[1].x});
    upsert = true;
    assertUpdateSucceeds(st, session, sessionDB, inTxn, queries[0], updates[0], upsert);
    assertHashedShardKeyUpdateCorrect(
        st, sessionDB, kDbName, queries[0], updates[0], upsert, false);
    assertUpdateSucceeds(st, session, sessionDB, inTxn, queries[1], updates[1], upsert);
    assertHashedShardKeyUpdateCorrect(st, sessionDB, kDbName, queries[1], updates[1], upsert, true);
    st.s.getDB(kDbName).foo.drop();
}
function assertCanUpdatePrimitiveShardKeyHashedChangeShards ( st , kDbName , ns , session , sessionDB , inTxn ) { let docsToInsert = [ { "x" : 4 , "a" : 3 } , { "x" : 78 } , { "x" : 100 } , { "x" : 300 , "a" : 3 } , { "x" : 500 , "a" : 6 } ] ; shardCollectionMoveChunks ( st , kDbName , ns , { "x" : "hashed" } , docsToInsert , { "x" : 100 } , { "x" : 300 } ) ; let docsOnShard0 = st . rs0 . getPrimary ( ) . getDB ( kDbName ) . foo . find ( ) . toArray ( ) ; let docsOnShard1 = st . rs1 . getPrimary ( ) . getDB ( kDbName ) . foo . find ( ) . toArray ( ) ; assert . gte ( docsOnShard0 . length , 2 ) ; assert . gte ( docsOnShard1 . length , 2 ) ; st . s . getDB ( kDbName ) . foo . remove ( { "x" : docsOnShard0 [ 1 ] . x } ) ; st . s . getDB ( kDbName ) . foo . remove ( { "x" : docsOnShard1 [ 1 ] . x } ) ; let queries = [ { "x" : docsOnShard0 [ 0 ] . x } , { "x" : docsOnShard1 [ 0 ] . x } ] ; let updates = [ { "$set" : { "x" : docsOnShard1 [ 1 ] . x } } , { "x" : docsOnShard0 [ 1 ] . x } ] ; let upsert = false ; assertUpdateSucceeds ( st , session , sessionDB , inTxn , queries [ 0 ] , updates [ 0 ] , upsert ) ; assertHashedShardKeyUpdateCorrect ( st , sessionDB , kDbName , queries [ 0 ] , updates [ 0 ] , upsert , false ) ; assertUpdateSucceeds ( st , session , sessionDB , inTxn , queries [ 1 ] , updates [ 1 ] , upsert ) ; assertHashedShardKeyUpdateCorrect ( st , sessionDB , kDbName , queries [ 1 ] , updates [ 1 ] , upsert , true ) ; st . s . getDB ( kDbName ) . foo . remove ( { "x" : docsOnShard0 [ 1 ] . x } ) ; st . s . getDB ( kDbName ) . foo . remove ( { "x" : docsOnShard1 [ 1 ] . x } ) ; upsert = true ; assertUpdateSucceeds ( st , session , sessionDB , inTxn , queries [ 0 ] , updates [ 0 ] , upsert ) ; assertHashedShardKeyUpdateCorrect ( st , sessionDB , kDbName , queries [ 0 ] , updates [ 0 ] , upsert , false ) ; assertUpdateSucceeds ( st , session , sessionDB , inTxn , queries [ 1 ] , updates [ 1 ] , upsert ) ; assertHashedShardKeyUpdateCorrect ( st , sessionDB , kDbName , queries [ 1 ] , updates [ 1 ] , upsert , true ) ; st . s . getDB ( kDbName ) . foo . drop ( ) ; }
commands that will change the shard key value and cause a document to move to a different shard.
commands that will change the shard key value and cause a document to move to a different shard .
// Because this collection is hash sharded, we need to figure out which values of x belong to // which shard. // Since we now know that the value of x in each of docsOnShard0[1] and docsOnShard1[1] will be // hashed to map to shard0 and shard1 respectively, we can delete these documents and then use // them as values to change the shard key to. // Non-upsert case. The first update will move a doc from shard0 to shard1 and the second will // move a doc from shard1 to shard 0. // Op-style modify // Replacement style modify // Upsert case. The first update will initially target shard0, but insert on shard1. The second // will initially target shard1, but insert on shard0. // Remove the docs we know we just inserted and then upsert them // Op-style upsert // Modify style upsert