Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| 
                    development:database:mongodb [2020/10/23 12:04] kalenpw created  | 
                
                    development:database:mongodb [2021/07/06 10:13] (current) kalenpw  | 
            ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| ===== Basics ===== | ===== Basics ===== | ||
| - | Runs on port: 27017 | + | Runs on port:   | 
| + | |||
| + | ---- | ||
| + | ===== Show databases ===== | ||
| + | <code mongodb> | ||
| + | show dbs | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | ===== Create DB ===== | ||
| + | <code mongodb> | ||
| + | use db_name # if db_name doesn' | ||
| + | # However, db_name will not be saved unless you create a collection | ||
| + | db.createCollection(" | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | ===== Create user ===== | ||
| + | <code mongodb> | ||
| + | # first user | ||
| + | use admin | ||
| + | > db.createUser( | ||
| + | ... { | ||
| + | ... user: " | ||
| + | ... pwd: passwordPrompt(), | ||
| + | ... roles: [ { role: " | ||
| + | ... } | ||
| + | ... ) | ||
| + | |||
| + | # user for specific database | ||
| + | use db_name | ||
| + | > db.createUser( | ||
| + | ... { | ||
| + | ... user: " | ||
| + | ... pwd: passwordPrompt(), | ||
| + | ... roles: [ | ||
| + | ... { role: " | ||
| + | ... ] | ||
| + | ... } | ||
| + | ... ) | ||
| + | |||
| + | </ | ||
| ---- | ---- | ||
| Line 14: | Line 55: | ||
| sudo apt update && sudo apt install mongodb-org | sudo apt update && sudo apt install mongodb-org | ||
| </ | </ | ||
| + | |||
| + | ---- | ||
| + | ===== Query ===== | ||
| + | <code javascript> | ||
| + | // Find all | ||
| + | db.authors.find(); | ||
| + | // find filter | ||
| + | db.authors.find({' | ||
| + | |||
| + | // count occurences | ||
| + | db.authors.countDocuments({}) // requires empty filter | ||
| + | </ | ||
| + | ---- | ||