Skip to content

Instantly share code, notes, and snippets.

View sarjarapu's full-sized avatar

sarjarapu

  • Amazon Web Services
  • Austin, TX
View GitHub Profile
@sarjarapu
sarjarapu / currentOp-adminCommand.js
Last active May 10, 2018 01:42
The MongoDB currentOp command via db.adminCommand
db.adminCommand({"currentOp": 1})
// or
db.adminCommand({"currentOp": 1, "$ownOps" : true})
@sarjarapu
sarjarapu / currentOp-wrapper.js
Last active May 10, 2018 01:42
The MongoDB currentOp command via a db.currentOp() wrapper function.
db.currentOp()
// or
db.currentOp({"$ownOps" : true})
@sarjarapu
sarjarapu / currentOp-collscan-queries.js
Last active May 10, 2018 01:43
The MongoDB currentOp command to help find queries not using any index
db.adminCommand({
"currentOp": true,
"op" : "query",
"planSummary": "COLLSCAN"
})
@sarjarapu
sarjarapu / currentOp-high-yield-ops.js
Created May 10, 2018 01:44
The MongoDB currentOp command to help you find database operations with number of yields greater than or equal to 100
db.adminCommand({
"currentOp": true,
"ns": /^guidebook\./,
"numYields" : {"$gte": 100}
})
@sarjarapu
sarjarapu / currentOp-waiting-for-lock.js
Last active May 10, 2018 01:46
The MongoDB currentOp command to help you find all the write operations that are waiting for a lock.
db.adminCommand({
"currentOp": true,
"waitingForLock" : true,
"$or": [
{ "op" : { "$in" : [ "insert", "update", "remove" ] } },
{ "query.findandmodify": { "$exists": true } }
]
})
@sarjarapu
sarjarapu / currentOp-long-running-ops.js
Last active May 16, 2018 20:23
The MongoDB currentOp command to help you find all the operations running longer than 300 milliseconds.
db.adminCommand({
"currentOp": true,
"microsecs_running" : {"$gte" : 300000}
})
/*
// output for the above command
{
"inprog": [{
"host": "shyam-macbook.local:27017",
"desc": "conn",
@sarjarapu
sarjarapu / labs-perf-restaurant-generate-load.js
Last active May 10, 2018 02:18
A JavaScript file to emulate some operations on 'restaurant' sample data set from MongoDB
function restaurant() {
var dbName = 'guidebook';
var cuisines = ['American', 'Chinese', 'Irish', 'Turkish', 'Italian', 'Mexican', 'Greek', 'Seafood', 'Indian', 'Japanese', 'Indian', 'Vegetarian', 'Hawaiian'];
var zipcodes = ["10030", "10471", "10112", "11426", "10039", "10035", "11005", "10307", "11692", "10044", "10026", "10280", "11233", "10103", "10121", "10282", "10473", "10281", "11436", "10153", "11433", "10057", "11242", "10111", "10122", "10168", "10107", "10000"];
function queryByZip(zipcode) {
var query = {"address.zipcode" : zipcode};
var coll = db.getSiblingDB(dbName).getCollection('restaurants');
var items = coll.find(query).toArray();
return items;
@sarjarapu
sarjarapu / currentOp-run-in-loop.js
Created May 10, 2018 02:05
A JavaScript function to execute currentOp in a while loop to capture operations over a period of time.
var i = 0;
while(i++<10) {
printjson(db.currentOp());
// sleep for 10 ms if required.
sleep(10)
}
@sarjarapu
sarjarapu / labs-perf-restaurant-practice.sh
Last active May 10, 2018 02:45
A bash script to help you download, import and generate load on the sample restaurant database.
#!/bin/sh
# download and import the restaurant sample data set
wget https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
mongoimport --db guidebook --collection restaurants --type json --file ./primer-dataset.json
# download and run the load generation script
wget https://goo.gl/BXHH2d -O labs-performance-restaurant.js
mongo guidebook --eval "load('./labs-performance-restaurant.js');r.performReads();" > /dev/null 2>&1 &
# open mongo shell and practice all the above use cases for currentOp
@sarjarapu
sarjarapu / userDefinedRole-getRole.js
Last active January 15, 2019 07:50
The MongoDB getRole command to show all the granted actions for readWrite role
// command to get readWrite privileges
db.getRole( "readWrite", { showPrivileges: true } ).privileges[0].actions
// output of above command
/*
[
"changeStream",
"collStats",
"convertToCapped",
"createCollection",
"createIndex",