Last active
October 13, 2017 05:25
-
-
Save ivanleoncz/7f937ab1425b264165cdda7664af7132 to your computer and use it in GitHub Desktop.
CRUD and general administration of MongoDB databases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Create, Read, Update and Delete | |
# SHOW DATABASES | |
> show databases | |
> show dbs | |
# DEFINE DATABASE FOR OPERATIONS | |
> use database_name | |
# CREATE DATABASE AND COLLECTIONS | |
> use world | |
> db.createCollection("Africa") | |
> db.createCollection("Asia") | |
> db.createCollection("Europe") | |
> db.createCollection("NorAmerica") | |
> db.createCollection("SouAmerica") | |
> db.createCollection("Antarctica") | |
> db.createCollection("Australia") | |
# RENAME COLLECTIONS | |
> db.NorAmerica.renameCollection("NorthAmerica") | |
> db.SouAmerica.renameCollection("SouthAmerica") | |
# CREATE DOCUMENTS | |
> db.Europe.insertOne( {"Country":"France","Population":"66.009.000"}) | |
> db.Europe.insertOne( {"Country":"CzechRepublic","Population":"10.056.000"}) | |
# CREATE MULTIPLE DOCUMENTS | |
> db.NorthAmerica.insertOne( [{"Country":"Mexico","Population":"127.005.000"},{"Country":"USA","Population":"323.001.000"}] ) | |
> db.Africa.insertOne( [{"Country":"Congo","Population":"78.074.000"},{"Country":"Angola","Population":"28.081.000"}] ) | |
# FIND DOCUMENT | |
> db.Europe.find({Country:"CzechRepublic"}) | |
# FIND DOCUMENT (Show Population FROM Country Mexico, without showing "_id" field) | |
> db.NorthAmerica.find({Country:"Mexico"},{Population:1,_id:0}) | |
# UPDATE DOCUMENT (For Country USA, Update Value of "Population" Key) | |
> db.NorthAmerica.update({Country:"USA"},{$set:{Population:"323.001.999"}}) | |
# UPDATE DOCUMENT - RENAME FIELD ON ALL DOCUMENTS | |
> db.Africa.update( {}, {$rename:{"Population":"Citizens"}}, {multi:true}) | |
# REMOVE DOCUMENT | |
> db.Europe.remove({Country:"CzechRepublic"}) | |
### System Management | |
# CREATE SUPER USER | |
> use admin | |
> db.createUser( {user:"mongo", pwd:"mongo", roles:["root"]} ) | |
# SHOW USERS | |
> use admin | |
> db.getUsers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment