Skip to content

Instantly share code, notes, and snippets.

@christkv
Last active December 29, 2015 02:29
Show Gist options
  • Save christkv/7601304 to your computer and use it in GitHub Desktop.
Save christkv/7601304 to your computer and use it in GitHub Desktop.
//
// Ensures that mongod respects the batch write protocols
//
var collectionName = "batch_write_protocol";
var coll = db.getCollection(collectionName);
jsTest.log("Starting insert tests...");
coll.remove({});
var request;
var result;
// Multiple docurments insert with errors, w:1 write concern specified, ordered:false
request = {insert : collectionName
, documents: [{a:1}]
, writeConcern:{w:1}
, ordered:false};
result = coll.runCommand(request)
print("========================================================== 0")
printjson(result)
// ========================================================== 0
// { "ok" : 1, "n" : 1 }
request = {insert : collectionName
, documents: [{a:1}]
, writeConcern:{w:1}
, ordered:false};
result = coll.runCommand(request)
print("========================================================== 1")
printjson(result)
// ========================================================== 1
// {
// "ok" : 0,
// "code" : 11000,
// "errmsg" : "E11000 duplicate key error index: test.batch_write_protocol.$a_1 dup key: { : 1.0 }",
// "n" : 0
// }
request = {insert : collectionName
, documents: [{a:1}, {a:1}]
, writeConcern:{w:1}
, ordered:false};
result = coll.runCommand(request)
print("========================================================== 1")
printjson(result)
// ========================================================== 1
// {
// "ok" : 0,
// "code" : 65,
// "errmsg" : "batch op errors occurred",
// "n" : 0,
// "errDetails" : [
// {
// "index" : 0,
// "code" : 11000,
// "errmsg" : "E11000 duplicate key error index: test.batch_write_protocol.$a_1 dup key: { : 1.0 }"
// 2013-11-22T16:01:30.131+0100 [conn2] end connection 127.0.0.1:62406 (0 connections now open)
// },
// {
// "index" : 1,
// "code" : 11000,
// "errmsg" : "E11000 duplicate key error index: test.batch_write_protocol.$a_1 dup key: { : 1.0 }"
// }
// ]
// }
// /**
// * Fail operation due to write concern
// */
// var batch = coll.initializeUnorderedBulkOp();
// batch.insert({a:1});
// batch.find({a:1}).updateOne({$set: {b:1}});
// batch.find({a:2}).upsert().updateOne({$set: {b:2}});
// batch.insert({a:3});
// var result = batch.execute({w:5, wtimeout:1});
// print("*******************************************************")
// printjson(result.getRawResponse())
// Repl wtimeout
// {
// "ok" : 0,
// "n" : 4,
// "code" : 64,
// "errmsg" : "timed out waiting for slaves",
// "errDetails" : [
// {
// "index" : 0,
// "code" : 64,
// "errmsg" : "timed out waiting for slaves",
// "op" : {
// "a" : 1
// },
// "errInfo" : {
// "wtimeout" : true
// }
// },
// {
// "index" : 1,
// "code" : 64,
// "errmsg" : "timed out waiting for slaves",
// "op" : {
// "q" : {
// "a" : 1
// },
// "u" : {
// "$set" : {
// "b" : 1
// }
// },
// "multi" : false,
// "upsert" : false
// },
// "errInfo" : {
// "wtimeout" : true
// }
// },
// {
// "index" : 3,
// "code" : 64,
// "errmsg" : "timed out waiting for slaves",
// "op" : {
// "a" : 3
// },
// "errInfo" : {
// "wtimeout" : true
// }
// }
// ],
// "upserted" : [
// {
// "index" : 2,
// "_id" : ObjectId("528f738cc400b0682aa7f38f")
// }
// ]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment