Skip to content

Latest commit

 

History

History
156 lines (69 loc) · 2.28 KB

mongodb.rst

File metadata and controls

156 lines (69 loc) · 2.28 KB

MongoDB shell commands

Installing MongoDB on linux/mac

brew install mongodb

Running the mongodb server

brew services start mongodb

Stop the mongodb server

brew services stop mongodb

Starting the mongodb shell in the terminal

mongo --host localhost:27017

Check server status and connection

db.serverStatus()

Create a database and use it

use testDatabase

Create 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 dbs

Show all the collections on the server

show collections

Delete 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.json

Delete current database

db.dropDatabase();

Show all the users

show users

Create a new user

db.createUser({"user": "testUser", "pwd": "testPassword", "roles": ["readWrite"]})

Log into the database using user credentials

mongo -u testUser -p testPassword --authenticationDatabase testDatabase

Delete a user

db.dropUser("testUser")

Importing a collection

mongoimport --db testDatabase --collection testCollection < path/to/db.json