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 / userDefinedRole-mlaunch.sh
Created May 16, 2018 14:30
A bash script to setup a MongoDB environment with authentication on your local machine
# Don't have mtools? The easiest way to install mtools is via pip
# pip install mtools
mlaunch init --auth --replicaset --port 28000
# output
# launching: "mongod" on port 28000
# launching: "mongod" on port 28001
# launching: "mongod" on port 28002
# replica set 'replset' initialized.
@sarjarapu
sarjarapu / userDefinedRole-createReadWriteUser.sh
Created May 16, 2018 14:33
A bash script to create a user with readWrite role on the social database
mongo admin --port 28000 -u user -p password --authenticationDatabase admin <<EOF
db.createUser({user: 'app_user', pwd: 'password', roles: [{role: 'readWrite', db: 'social'}]})
EOF
# The output of above command
# MongoDB shell version v3.6.2
# connecting to: mongodb://127.0.0.1:28000/
# MongoDB server version: 3.6.2
# Successfully added user: {
# "user" : "app_user",
@sarjarapu
sarjarapu / userDefinedRole-readWrite-drop.sh
Last active May 16, 2018 14:38
Set of bash & JavaScript to show that user with readWrite role can drop a collection
mongo social --port 28000 -u app_user -p password --authenticationDatabase admin
# output of above command
# MongoDB shell version v3.6.2
# connecting to: mongodb://127.0.0.1:28000/social
# MongoDB server version: 3.6.2
# replset:PRIMARY>
# create a document in person collection
db.person.insert({"fname": "Shyam", "lname": "Arjarapu"})
# output of above command
@sarjarapu
sarjarapu / userDefinedRole-readWriteMinusDropRole.sh
Created May 16, 2018 14:41
Set of bash & JavaScript to show that user with user-defined role, readWriteMinusDropRole role cannot drop a collection
mongo social --port 28000 -u human_user -p password --authenticationDatabase admin
# output of above command
# MongoDB shell version v3.6.2
# connecting to: mongodb://127.0.0.1:28000/social
# MongoDB server version: 3.6.2
# replset:PRIMARY>
# create a document in person collection
db.person.insert({"fname": "Shyam", "lname": "Arjarapu"})
# output of above command
@sarjarapu
sarjarapu / userDefinedRole-createRole.sh
Created May 16, 2018 14:42
A bash script with MongoDB commands to create a user-defined role and a user.
mongo social --port 28000 -u user -p password --authenticationDatabase admin <<EOF
db.createRole({
role: "readWriteMinusDropRole",
privileges: [
{
resource: { db: "social", collection: ""},
actions: [ "collStats", "dbHash", "dbStats", "find", "killCursors", "listIndexes", "listCollections", "convertToCapped", "createCollection", "createIndex", "dropIndex", "insert", "remove", "renameCollectionSameDB", "update"]} ],
roles: []
}
);
@sarjarapu
sarjarapu / transactions-environment.sh
Created May 28, 2018 20:14
A bash script with download MongoDB v4.0 release candidate and create a 1 member replica set
curl -O https://downloads.mongodb.com/osx/mongodb-osx-x86_64-enterprise-4.0.0-rc0.tgz
tar -xvzf mongodb-osx-x86_64-enterprise-4.0.0-rc0.tgz
rm mongodb-osx-x86_64-enterprise-4.0.0-rc0.tgz
mv mongodb-osx-x86_64-enterprise-4.0.0-rc0 v4.0.0-rc0
mkdir data
v4.0.0-rc0/bin/mongod --dbpath data --logpath data/mongod.log --fork --replSet rs0 --port 38000
v4.0.0-rc0/bin/mongo --port 38000 --eval "rs.initiate()"
@sarjarapu
sarjarapu / transactions-commitTransaction.js
Created May 28, 2018 20:22
MongoDB shell commands illustrating the newly added document in the transaction is not visible on the db.person collection object until the session1 is committed.
// v4.0.0-rc0/bin/mongo --port 38000
// ****************************************************
// On a sample person collection with two documents _id 1, 2
// Insert new document, _id 3, inside session1 scope
// Understand how the find operation on these scopes change
// from startTransaction to commitTransaction
// ****************************************************
// drop and recreate person collection with 2 documents _id 1, 2
@sarjarapu
sarjarapu / transactions-abortTransaction.js
Created May 28, 2018 20:25
MongoDB shell commands illustrating the newly added document in the transaction is not visible on the db.person or session1 person collection objects when the session1 is aborted.
// v4.0.0-rc0/bin/mongo --port 38000
// ****************************************************
// On a sample person collection with two documents _id 1, 2
// Insert new document, _id 3, inside session1 scope
// Understand how the find operation on these scopes change
// from startTransaction to abortTransaction
// ****************************************************
// drop and recreate person collection with 2 documents _id 1, 2
@sarjarapu
sarjarapu / transactions-no-writeconflict.js
Last active May 28, 2018 20:58
MongoDB commands illustrating multiple transactions with no write conflicts can successfully commitTransactions
// v4.0.0-rc0/bin/mongo --port 38000
// ****************************************************
// On a sample person collection with two documents _id 1, 2
// Insert new document, _id 3, inside session1 scope
// Update a document, _id 1, in session2 scope
// Delete a document, _id 2, directly on collection
// Understand how the find operation on these scopes change
// from beginning of transaction till after commit
// ****************************************************
@sarjarapu
sarjarapu / transactions-writeconflicts.js
Created May 28, 2018 21:02
MongoDB commands illustrating multiple transactions with write conflicts are aborted
// v4.0.0-rc0/bin/mongo --port 38000
// ****************************************************
// On a sample person collection with two documents _id 1, 2
// Update a document, _id 1, in session1 scope
// Delete a document, _id 2, in session1 scope
// Delete a document, _id 2, in session2 scope
// Understand how the find operation on these scopes change
// ****************************************************