Skip to content

Instantly share code, notes, and snippets.

@josdem
Last active May 19, 2024 11:50
Show Gist options
  • Save josdem/b0cdb57dbf739d9072a0 to your computer and use it in GitHub Desktop.
Save josdem/b0cdb57dbf739d9072a0 to your computer and use it in GitHub Desktop.
//Print a list of all databases on the server
show dbs
//Switch or create database
use {database}
//Print a list of all collections for current database
db.getCollectionNames()
//Find all documents in the collection and returns a cursor
db.{collection}.find()
//Drops or removes completely the collection
db.{collection}.drop()
//Create user
use databasename
db.createUser(
{
user: "username",
pwd: "password",
roles: [
{ role: "readWrite", db: "databasename" }
]
}
)
//Connect to your database as custom user
mongosh databasename -u username -p password --authenticationDatabase "databasename"
//select a row in a collection
db.{collection}.find({"fieldName": "value"})
//Update example
db.beverage.updateOne(
{
"_id": 86
},
{
$set:{
"image": "https://storage.googleapis.com/jugoterapia/86.png"
}
}
);
//Insert a row
db.beverage.insert( { "_id" : 113, "name" : "Anti-reflux", "ingredients" : "2 aloe vera gel tablespoons,1/2 cup of water,Honey to taste", "recipe" : "Aloe vera is gel from the leaves and helps to reduce swollen and burning symptoms from reflux. Add all ingredients to the blender, and drink it after every meal.", "image" : "", "categoryId" : 5 } )
//Backup a database
mongodump --db jugoterapia --out ~/databases/jugoterapia_20231206
//Export a collection
mongoexport --collection=category --db=jugoterapia --out=category.json
//Show user privileges
db.getUser("josdem")
//Delete user
db.dropUser("josdem")
Run:
```
sudo systemctl start mongod
```
Verify:
```
sudo systemctl status mongod
```
### Mac
To install:
```
brew install mongodb
```
To Run:
```
brew services start mongodb
```
To see mongo configuration:
```
sudo vim /usr/local/etc/mongod.conf
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment