Skip to content

Instantly share code, notes, and snippets.

@renctan
Last active December 18, 2015 08:19
Show Gist options
  • Save renctan/5752715 to your computer and use it in GitHub Desktop.
Save renctan/5752715 to your computer and use it in GitHub Desktop.
Pick one scenario to run by removing the comments at the end of the script.
var st = new ShardingTest({ shards: 2 });
st.adminCommand({ enablesharding: "test" });
st.adminCommand({ shardcollection: "test.col0", key: { a: 1, b: 1 }});
st.adminCommand({ shardcollection: "test.col1", key: { 'x.a': 1 }});
var db = st.s.getDB( "test" );
var col = db.getCollection( "col0" );
var dotCol = db.getCollection( "col1" );
var emptyQuery = function() {
print('empty query update');
col.remove({}, false);
col.insert({ a: 100, b: 100 });
printjson(col.findOne());
col.update({}, { a: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({}, { a: 1, b: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({}, { a: 1, b: 1, _id: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({}, { $set: { a: 1, b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({}, { $set: { a: 1, b: 1, _id: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({}, { $set: { c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var emptyQueryUpsert = function() {
print('empty query upsert');
col.remove({}, false);
col.update({}, { a: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({}, { a: 1, b: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({}, { a: 1, b: 1, _id: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({}, { $set: { a: 1, b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({}, { $set: { a: 1, b: 1, _id: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({}, { $set: { c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var partialSKey = function() {
print('partial skey query update');
col.remove({}, false);
col.insert({ a: 100, b: 100 });
printjson(col.findOne());
col.update({ a: 100 }, { a: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { a: 2 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { a: 1, b: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { a: 1, b: 1, _id: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { $set: { a: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { $set: { b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { $set: { a: 1, b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { $set: { a: 1, b: 1, _id: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100 }, { $set: { c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var partialSKeyUpsert = function() {
print('partial skey query upsert');
col.remove({}, false);
printjson(col.findOne());
col.update({ a: 100 }, { a: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { a: 2 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { a: 1, b: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { a: 1, b: 1, _id: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { $set: { a: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { $set: { b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { $set: { a: 1, b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { $set: { a: 1, b: 1, _id: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100 }, { $set: { c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var noSkeyPrefix = function() {
print('not prefix of skey query update');
col.remove({}, false);
col.insert({ a: 100, b: 100 });
printjson(col.findOne());
col.update({ b: 100 }, { b: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { b: 2 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { a: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { a: 1, b: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { a: 1, b: 1, _id: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { $set: { b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { $set: { a: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { $set: { a: 1, b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { $set: { a: 1, b: 1, _id: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ b: 100 }, { $set: { c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var noSkeyPrefixUpsert = function() {
print('not prefix of skey query upsert');
col.remove({}, false);
printjson(col.findOne());
col.update({ b: 100 }, { b: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { b: 2 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { a: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { a: 1, b: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { a: 1, b: 1, _id: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { $set: { b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { $set: { a: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { $set: { a: 1, b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { $set: { a: 1, b: 1, _id: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ b: 100 }, { $set: { c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var fullSkey = function() {
print('full skey query update');
col.remove({}, false);
col.insert({ a: 100, b: 100 });
printjson(col.findOne());
col.update({ a: 100, b: 100 }, { a: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { a: 100, b: 100, c: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { a: 100, b: 100, _id: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { b: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { $set: { b: 100, c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { $set: { a: 100, b: 100, c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { $set: { a: 100, b: 100, _id: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { $set: { a: 100, b: 2, c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ a: 100, b: 100 });
col.update({ a: 100, b: 100 }, { $set: { c: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var fullSkeyUpsert = function() {
print('full skey query upsert');
col.remove({}, false);
printjson(col.findOne());
col.update({ a: 100, b: 100 }, { a: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { a: 100, b: 100, c: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { a: 100, b: 100, _id: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { b: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { $set: { b: 100, c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { $set: { a: 100, b: 100, c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { $set: { a: 100, b: 100, _id: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { $set: { a: 100, b: 2, c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ a: 100, b: 100 }, { $set: { c: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var idQuery = function() {
print('_id query update');
col.remove({}, false);
col.insert({ _id: 1, a: 100, b: 100 });
printjson(col.findOne());
col.update({ _id: 1 }, { a: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ _id: 1, a: 100, b: 100 });
col.update({ _id: 1 }, { a: 1, b: 1 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ _id: 1, a: 100, b: 100 });
col.update({ _id: 1 }, { $set: { a: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ _id: 1, a: 100, b: 100 });
col.update({ _id: 1 }, { $set: { b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.insert({ _id: 1, a: 100, b: 100 });
col.update({ _id: 1 }, { $set: { a: 1, b: 1 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var idQueryUpsert = function() {
print('_id query upsert');
col.remove({}, false);
printjson(col.findOne());
col.update({ _id: 1 }, { a: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ _id: 1 }, { a: 1, b: 1 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ _id: 1 }, { $set: { a: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ _id: 1 }, { $set: { b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
col.remove({}, false);
col.update({ _id: 1 }, { $set: { a: 1, b: 1 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(col.findOne());
};
var dottedUpdate = function() {
print('Dotted query update');
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { x: { a: 100, b: 2 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
assert.throws(function() {
dotCol.update({ 'x.a': 100 }, { x: { 'a.z': 100 }});
});
print('threw 1');
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
assert.throws(function() {
dotCol.update({ 'x.a': 100 }, { 'x.a': 100 });
});
print('threw 2');
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
assert.throws(function() {
dotCol.update({ 'x.a': 100 }, { 'x.a.z': 100 });
});
print('threw 3');
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { x: 100 });
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { x: { b: 100 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { x: { a: 100, b: 2 }}});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { x: { a: 2 }}});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { 'x.a': 100, b: 3 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { 'x.a': 2 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { x: { 'a.z': 100 }}});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { 'x.a.z': 100 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { x: 100 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.remove({}, false);
dotCol.insert({ x: { a: 100 }});
dotCol.update({ 'x.a': 100 }, { $set: { 'x.b': 200 }});
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
};
var dottedUpsert = function() {
print('Dotted query upsert');
dotCol.remove({}, false);
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { x: { a: 100, b: 2 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
assert.throws(function() {
dotCol.update({ 'x.a': 100 }, { x: { 'a.z': 100 }}, true);
});
print('threw 1');
dotCol.remove({}, false);
assert.throws(function() {
dotCol.update({ 'x.a': 100 }, { 'x.a': 100 }, true);
});
print('threw 2');
dotCol.remove({}, false);
assert.throws(function() {
dotCol.update({ 'x.a': 100 }, { 'x.a.z': 100 }, true);
});
print('threw 3');
dotCol.update({ 'x.a': 100 }, { x: 100 }, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { x: { b: 100 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { x: { a: 100, b: 2 }}}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { x: { a: 2 }}}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { 'x.a': 100, b: 3 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { 'x.a': 2 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { x: { 'a.z': 100 }}}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { 'x.a.z': 100 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { x: 100 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
dotCol.update({ 'x.a': 100 }, { $set: { 'x.b': 200 }}, true);
printjson(db.runCommand({ getLastError: 1 }));
printjson(dotCol.findOne());
};
//emptyQuery();
//emptyQueryUpsert();
//partialSKey();
//partialSKeyUpsert();
//noSkeyPrefix();
//noSkeyPrefixUpsert();
//fullSkey();
//fullSkeyUpsert();
//idQuery();
//idQueryUpsert();
//dottedUpdate();
//dottedUpsert();
st.stop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment