Installing MongoDB on linux/mac
brew install mongodbRunning the mongodb server
brew services start mongodbStop the mongodb server
brew services stop mongodbStarting the mongodb shell in the terminal
mongo --host localhost:27017Check server status and connection
db.serverStatus()Create a database and use it
use testDatabaseCreate a collection (table)
db.createCollection("testCollection")Insert one document in the collection
db.testCollection.insert({"city": "San Fransisco", "population": "884363", "state": "California"})Insert multiple documents in the database
db.testCollection.insert([{"city": "San Fransisco", "population": "884363", "state": "California"},{"city": "Seattle", "population": "724745", "state": "Washington"},{"city": "Portland", "population": "647805", "state": "Oregon"}])List all the records in the collection
db.testCollection.find()Search for a specific record
db.user.find({"city": "Seattle"})Show all the databases on the server
show dbsShow all the collections on the server
show collectionsDelete a collection
db.testCollection.drop()Show the total size of the collection
db.testColection.dataSize()Exporting a collection
mongoexport --db testDatabase --collection testCollection --out db.jsonDelete current database
db.dropDatabase();Show all the users
show usersCreate a new user
db.createUser({"user": "testUser", "pwd": "testPassword", "roles": ["readWrite"]})Log into the database using user credentials
mongo -u testUser -p testPassword --authenticationDatabase testDatabaseDelete a user
db.dropUser("testUser")Importing a collection
mongoimport --db testDatabase --collection testCollection < path/to/db.json