[MongoDB]: How to simulate read/write transactions manually
[root@dbversity ~]$ /opt/mongodb/bin/mongo
MongoDB shell version: 2.6.9
connecting to: test
>
> // To simulate write (inserts) transactions
> for(var i = 1; i <= 10000000 ; i++){db.test_collection.insert({“_id” : i , “action” : “write transaction simulations”, ” iteration no:” : i });}
WriteResult({ “nInserted” : 1 })
>
> // To simulate read transactions
> for(var i =1; i <= 10000000 ; i++){db.test_collection.find({“_id” : i });}
>
> for(var i =1; i <= 10000 ; i++){db.test_collection.find({“_id” : i });}
{ “_id” : 10000, “action” : “write transaction simulations”, ” iteration no:” : 10000 }
>
> show collections
system.indexes
test_collection
>
>
> db.test_collection.findOne()
{
“_id” : 1,
“action” : “write transaction simulations”,
” iteration no:” : 1
}
>
// To simulate write (update) transactions
> for(var i = 1; i <= 10000 ; i++){db.test_collection.update({_id : i} , { $set : {“action” : “update transactions” , ” iteration no:” : i } }, {multi:true}); }
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
>
>
>
> // for infinite no. of updates, please use without where condition as below. There might be 10000*10000 writes for below query.
> // for(var i = 1; i <= 10000 ; i++){db.test_collection.update({}, { $set : {“action” : “update transactions” , ” iteration no:” : i } }, {multi:true}); }
>
// To simulate write (inserts/updates) transactions
> for(var i = 1; i <= 100 ; i++){db.test_collection.update({ _id: i}, { $set : {“action” : “new update transactions” , “new_field” : i } }, {multi:true, upsert: true} ); }
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
>
> db.test_collection.find().limit(3)
{ “_id” : 1, “action” : “new update transactions”, ” iteration no:” : 1, “new_field” : 1 }
{ “_id” : 2, “action” : “new update transactions”, ” iteration no:” : 2, “new_field” : 2 }
{ “_id” : 3, “action” : “new update transactions”, ” iteration no:” : 3, “new_field” : 3 }
>
// To simulate read & write (update/insert) transactions
> for(var i = 1; i <= 100 ; i++){db.test_collection.update({ _id : i}, { $set : {“action” : “very new update transactions” , “latest_field:” : i } }, {multi:true, upsert: true} ); db.test_collection.find({“_id” : i });}
{ “_id” : 100, “action” : “very new update transactions”, ” iteration no:” : 100, “new_field” : 100, “latest_field:” : 100 }
>
>
>
> db.test_collection.find().limit(3)
{ “_id” : 101, “action” : “update transactions”, ” iteration no:” : 101 }
{ “_id” : 102, “action” : “update transactions”, ” iteration no:” : 102 }
{ “_id” : 103, “action” : “update transactions”, ” iteration no:” : 103 }
>
>
> db.test_collection.find({_id:100})
{ “_id” : 100, “action” : “very new update transactions”, ” iteration no:” : 100, “new_field” : 100, “latest_field:” : 100 }
>
>
> db.test_collection.find({_id:101})
{ “_id” : 101, “action” : “update transactions”, ” iteration no:” : 101 }
>
>
To check the current Operations on the database,
}
> db.currentOp()
{
“inprog” : [
{
“opid” : 5958602,
“active” : true,
“secs_running” : 0,
“microsecs_running” : NumberLong(214575),
“op” : “update”,
“ns” : “test.test_collection”,
“query” : {
},
“client” : “127.0.0.1:46473”,
“desc” : “conn4”,
“threadId” : “0x7fade4f1e700”,
“connectionId” : 4,
“locks” : {
“^” : “w”,
“^test” : “W”
},
“waitingForLock” : false,
“numYields” : 2,
“lockStats” : {
“timeLockedMicros” : {
“r” : NumberLong(0),
“w” : NumberLong(398741)
},
“timeAcquiringMicros” : {
“r” : NumberLong(0),
“w” : NumberLong(2831)
}
}
}
]
}
> db.printCollectionStats()
test_collection
{
“ns” : “test.test_collection”,
“count” : 100000,
“size” : 11200000,
“avgObjSize” : 112,
“storageSize” : 22507520,
“numExtents” : 7,
“nindexes” : 1,
“lastExtentSize” : 11325440,
“paddingFactor” : 1,
“systemFlags” : 1,
“userFlags” : 1,
“totalIndexSize” : 2812544,
“indexSizes” : {
“_id_” : 2812544
},
“ok” : 1
}
—
>
> db.serverStatus()
{
“host” : “vm-5cc5-bad6”,
“version” : “2.6.9”,
“process” : “mongod”,
“pid” : NumberLong(22305),
“uptime” : 4716,
“uptimeMillis” : NumberLong(4716007),
“uptimeEstimate” : 4633,
“localTime” : ISODate(“2015-07-09T06:19:01.989Z”),
“asserts” : {
“regular” : 0,
“warning” : 0,
“msg” : 0,
“user” : 0,
“rollovers” : 0
},
“backgroundFlushing” : {
“flushes” : 78,
“total_ms” : 1223,
“average_ms” : 15.679487179487179,
“last_ms” : 44,
“last_finished” : ISODate(“2015-07-09T06:18:26.368Z”)
},
“connections” : {
“current” : 3,
“available” : 816,
“totalCreated” : NumberLong(5)
},
“cursors” : {
“note” : “deprecated, use server status metrics”,
“clientCursors_size” : 0,
“totalOpen” : 0,
“pinned” : 0,
“totalNoTimeout” : 0,
“timedOut” : 1
},
“dur” : {
“commits” : 28,
“journaledMB” : 2.49856,
“writeToDataFilesMB” : 4.221184,
“compression” : 0.2367582270915976,
“commitsInWriteLock” : 0,
“earlyCommits” : 0,
“timeMs” : {
“dt” : 3057,
“prepLogBuffer” : 41,
“writeToJournal” : 94,
“writeToDataFiles” : 44,
“remapPrivateView” : 1
}
},
“extra_info” : {
“note” : “fields vary by platform”,
“heap_usage_bytes” : 64254840,
“page_faults” : 74
},
“globalLock” : {
“totalTime” : NumberLong(“4716015000”),
“lockTime” : NumberLong(6998088),
“currentQueue” : {
“total” : 0,
“readers” : 0,
“writers” : 0
},
“activeClients” : {
“total” : 1,
“readers” : 0,
“writers” : 1
}
},
“indexCounters” : {
“accesses” : 11530951,
“hits” : 11530951,
“misses” : 0,
“resets” : 0,
“missRatio” : 0
},
“locks” : {
“.” : {
“timeLockedMicros” : {
“R” : NumberLong(9584070),
“W” : NumberLong(6998088)
},
“timeAcquiringMicros” : {
“R” : NumberLong(5607465),
“W” : NumberLong(253002)
}
},
“admin” : {
“timeLockedMicros” : {
“r” : NumberLong(46900),
“w” : NumberLong(0)
},
“timeAcquiringMicros” : {
“r” : NumberLong(173),
“w” : NumberLong(0)
}
},
“local” : {
“timeLockedMicros” : {
“r” : NumberLong(52595),
“w” : NumberLong(0)
},
“timeAcquiringMicros” : {
“r” : NumberLong(9731),
“w” : NumberLong(0)
}
},
“dbversity_db” : {
“timeLockedMicros” : {
“r” : NumberLong(13190),
“w” : NumberLong(47707441)
},
“timeAcquiringMicros” : {
“r” : NumberLong(9047),
“w” : NumberLong(4144561)
}
},
“test” : {
“timeLockedMicros” : {
“r” : NumberLong(5717),
“w” : NumberLong(1282973644)
},
“timeAcquiringMicros” : {
“r” : NumberLong(118628),
“w” : NumberLong(12249622)
}
}
},
“network” : {
“bytesIn” : 371176824,
“bytesOut” : 111322617,
“numRequests” : 1986559
},
“opcounters” : {
“insert” : 1985096,
“query” : 303,
“update” : 1178,
“delete” : 0,
“getmore” : 0,
“command” : 233
},
“opcountersRepl” : {
“insert” : 0,
“query” : 0,
“update” : 0,
“delete” : 0,
“getmore” : 0,
“command” : 0
},
“recordStats” : {
“accessesNotInMemory” : 0,
“pageFaultExceptionsThrown” : 0,
“admin” : {
“accessesNotInMemory” : 0,
“pageFaultExceptionsThrown” : 0
},
“dbversity_db” : {
“accessesNotInMemory” : 0,
“pageFaultExceptionsThrown” : 0
},
“local” : {
“accessesNotInMemory” : 0,
“pageFaultExceptionsThrown” : 0
},
“test” : {
“accessesNotInMemory” : 0,
“pageFaultExceptionsThrown” : 0
}
},
“writeBacksQueued” : false,
“mem” : {
“bits” : 64,
“resident” : 355,
“virtual” : 1649,
“supported” : true,
“mapped” : 624,
“mappedWithJournal” : 1248
},
“metrics” : {
“cursor” : {
“timedOut” : NumberLong(1),
“open” : {
“noTimeout” : NumberLong(0),
“pinned” : NumberLong(0),
“total” : NumberLong(0)
}
},
“document” : {
“deleted” : NumberLong(0),
“inserted” : NumberLong(1985096),
“returned” : NumberLong(242),
“updated” : NumberLong(117500001)
},
“getLastError” : {
“wtime” : {
“num” : 0,
“totalMillis” : 0
},
“wtimeouts” : NumberLong(0)
},
“operation” : {
“fastmod” : NumberLong(1174),
“idhack” : NumberLong(0),
“scanAndOrder” : NumberLong(0)
},
“queryExecutor” : {
“scanned” : NumberLong(117500000),
“scannedObjects” : NumberLong(117500000)
},
“record” : {
“moves” : NumberLong(0)
},
“repl” : {
“apply” : {
“batches” : {
“num” : 0,
“totalMillis” : 0
},
“ops” : NumberLong(0)
},
“buffer” : {
“count” : NumberLong(0),
“maxSizeBytes” : 268435456,
“sizeBytes” : NumberLong(0)
},
“network” : {
“bytes” : NumberLong(0),
“getmores” : {
“num” : 0,
“totalMillis” : 0
},
“ops” : NumberLong(0),
“readersCreated” : NumberLong(0)
},
“preload” : {
“docs” : {
“num” : 0,
“totalMillis” : 0
},
“indexes” : {
“num” : 0,
“totalMillis” : 0
}
}
},
“storage” : {
“freelist” : {
“search” : {
“bucketExhausted” : NumberLong(0),
“requests” : NumberLong(1991923),
“scanned” : NumberLong(3983810)
}
}
},
“ttl” : {
“deletedDocuments” : NumberLong(0),
“passes” : NumberLong(78)
}
},
“ok” : 1
}
>
> db.stats()
{
“db” : “test”,
“collections” : 3,
“objects” : 100004,
“avgObjSize” : 111.99808007679692,
“dataSize” : 11200256,
“storageSize” : 22523904,
“numExtents” : 9,
“indexes” : 1,
“indexSize” : 2812544,
“fileSize” : 67108864,
“nsSizeMB” : 16,
“dataFileVersion” : {
“major” : 4,
“minor” : 5
},
“extentFreeList” : {
“num” : 0,
“totalSize” : 0
},
“ok” : 1
}
>
> db.getLastError()
null
>
> db.currentOp()
{
“inprog” : [
{
“opid” : 32024,
“active” : true,
“secs_running” : 0,
“microsecs_running” : NumberLong(50071),
“op” : “update”,
“ns” : “test.test_collection”,
“query” : {
},
“client” : “127.0.0.1:47694”,
“desc” : “conn1”,
“threadId” : “0x7fdfb4c91700”,
“connectionId” : 1,
“locks” : {
“^” : “w”,
“^test” : “W”
},
“waitingForLock” : false,
“numYields” : 1,
“lockStats” : {
“timeLockedMicros” : {
“r” : NumberLong(0),
“w” : NumberLong(69147)
},
“timeAcquiringMicros” : {
“r” : NumberLong(0),
“w” : NumberLong(5927)
}
}
}
]
}
>