Skip to content

Instantly share code, notes, and snippets.

@deeptiGarg
Last active April 1, 2019 01:56
Show Gist options
  • Save deeptiGarg/f7992992c16f8071f467a1a139b7648a to your computer and use it in GitHub Desktop.
Save deeptiGarg/f7992992c16f8071f467a1a139b7648a to your computer and use it in GitHub Desktop.
Describes the MongoDB installation on GCP and getting started.
Go to GCP MarketPlace.
Search for MongoDb machine image.
Install it based on your disk requirements. ( You can select the number of Arbiters needed)
Run the Compute engines for MongoDB Server and Arbiter(s).
SSH on Server.
Run MongoDB shell by typing 'mongo'.
-------------------------------------Commands-----------------------------------
Get all the databases - db.adminCommand( { listDatabases: 1 } )
Select DB to use - use "DBName"
View all collections in the current db - db.getCollectionInfos() ; Example CollectionName = mydb
To insert document in collection - db.mydb.insert({ item: "card", qty: 15 })
View all documents in the collection - db.mydb.find( {} ) (Equivalent to select * from mydb in RDBMS)
---------------------------------
:wq save and exit
:q exit
i to enter edit mode
vim <fileName> to open the file on terminal // default-read only mode
------
****DB authentication Enablement****
Open port 27017 - used by MongoDb
db.createUser({
user: 'dp',
pwd: '123456',
roles: [{ role: 'readWrite', db:'deepti'}]
})
*****Enable authentication connection in config file to the DB*******
sudo vim /etc/mongod.conf
uncomment - security:
authorization: 'enabled'
sudo service mongod restart
******Access DB using**********
mongo -u dp -p 123456 deepti
**********Export collection as csv************ (not from mongo shell)
mongoexport -u dp -p 123456 --db deepti --collection drugs --type csv --out drugsData.csv --fields genericName, pediatricUse,indication
*********To delete a mongod on a particular port****************
mongo --port 27022
use admin;
db.shutdownServer();
********SPL Related work*******************************
Count number of distinct noOfSentences and show in descending order - db.drugs.aggregate( {$group : { _id : '$noOfSentence', count : {$sum : 1}}},{ $sort: { count: -1 } } )
@deeptiGarg
Copy link
Author

Describes the MongoDB installation on Google Cloud Platform and getting started on MongoDB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment