Basic commands MongoDB
Tags: mongodb, beginners, webdev, database
Published: 2022-07-06 19:15:44

# Summary

01. [Select/create database](#selectcreate-database)
00. [Insert/create collection/table with data](#insertcreate-collectiontable-with-data)
00. [Show created databases](#show-created-databases)
00. [Show all collections in the database](#show-all-collections-in-the-database)
00. [Select data in the collection](#select-data-in-the-collection)
00. [Update data from one registry/document](#update-data-from-one-registrydocument)
00. [Update all data from one registry/document](#update-all-data-from-one-registrydocument)
00. [Delete data in the collection](#delete-data-in-the-collection)
00. [Delete database](#delete-database)

# Select/create database
```
use firstDatabase;
```

# Insert/create collection/table with data
## Syntax
```C#
//INSERT one data in the collection
db.NameOfCollection.insertOne({"user": "admin", "password": "admin"});

//INSERT several data in the collection
db.NameOfCollection.insertMany([
  {"user": "admin", "password": "admin"},
  {"user": "admin", "password": "admin"},
  {"user": "admin", "password": "admin", "age": "10"}
]);
```
## Example
```C#
//INSERT one data in the collection user
db.user.insertOne({"user": "admin", "password": "admin"});

//INSERT one data in the collection person
db.person.insertOne({"name": "Bill Gates", "age": "66 "});

//INSERT several data in the collection user
db.user.insertMany([
  {"user": "admin1", "password": "admin1"}, 
  {"user": "admin2", "password": "admin2"},
  {"user": "admin1", "password": "password"}
]);

/*INSERT several data in other collection
in this case collection person*/
db.person.insertMany([
  {"name": "Joao", "age": "10"}, 
  {"name": "Joao", "age": "20"},
  {"name": "Lucas", "age": "15"}
]);

```

# Show created databases
**Only show the database if something was inserted in some collection**
## Syntax
```C#
show dbs;
```
## Example
```C++
//Result example:
admin    0.000GB
config   0.000GB
local    0.000GB
firstDatabase  0.000GB
```

# Show all collections in the database
## Syntax
``` C#
show collections;
```
## Example
```C#
//Result example:
person
user
```

# Select data in the collection
## Syntax
```C#
//Select all the register of one collection equal to SELECT all
db.NameOfCollection.find(); 

//SELECT the first register in the collection
db.NameOfCollection.findOne(); 

//SELECT all when the parameter is true
db.NameOfCollection.find({"WHERE"}); 

//SELECT all with the result similar to a Json (Bson)
db.NameOfCollection.find().pretty(); 
```
## Example
```C#
//Data
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "user" : "admin1", "password" : "admin1" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"), "user" : "admin2", "password" : "admin2" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }

//SELECT the first with user = admin1
db.user.findOne({"user": "admin1"}); 

//Result
{
        "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"),
        "user" : "admin1",
        "password" : "admin1"
}

//SELECT all with user = admin1
db.user.find({"user": "admin1"}); 

//Result
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "user" : "admin1", "password" : "admin1" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }

//SELECT all and return a Bson
db.usuario.find().pretty(); 

//Result
{
        "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"),
        "user" : "admin1",
        "password" : "admin1"
}
{
        "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"),
        "user" : "admin2",
        "password" : "admin2"
}
{
        "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"),
        "user" : "admin1",
        "password" : "password"
}
```

# Update data from one registry/document
## Syntax
```C#
/*Update the data when the first parameter is true to the second parameter 
don't change the struct*/

//Update one when the first parameter is true
db.NameOfCollection.updateOne({"WHERE"}, {$set:"SET"}); 

//Update all when the first parameter is true
db.NameOfCollection.updateMany({"WHERE"}, {$set:"SET"});
```
## Example
```C#
//Data
db.user.insertMany([
  {"user": "admin1", "password": "admin1"}, 
  {"user": "admin2", "password": "admin2"},
  {"user": "admin1", "password": "password"}
]);

//UPDATE one when the first parameter is true
db.firstDatabase.updateOne({"user" : "admin1"}, {$set: {"password": "new-password"}}); 

//Result
db.user.insertMany([
  {"user": "admin1", "password": "new-password"}, 
  {"user": "admin2", "password": "admin2"},
  {"user": "admin1", "password": "password"}
]);

//UPDATE all when the first parameter is true
db.user.updateMany({"user" : "admin1"}, {$set: {"password": "new-password"}}); 

//Result
db.user.insertMany([
  {"user": "admin1", "password": "new-password"}, 
  {"user": "admin2", "password": "admin2"},
  {"user": "admin1", "password": "new-password"}
]);
```

# Update all data from one registry/document
## Syntax
```C#
/*Change ALL data when the first parameter is true to the second parameter 
change the struct, similar to ALTER TABLE*/

//Change just the first when the parameter is true
db.NameOfCollection.replaceOne({"WHERE"}, {"Nova estrutura"});
```
``` replaceMany ``` **Don't exist**
## Example
```C#
//Data
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "user" : "admin1", "password" : "admin1" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"), "user" : "admin2", "password" : "admin2" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }

/*Alter all data from the first when user = admin1
  to "password": "new-password"*/
db.user.replaceOne({"user" : "admin1"}, {"password": "new-password"}); 

//Result
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca1"), "password" : "new-password" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca2"), "user" : "admin2", "password" : "admin2" }
{ "_id" : ObjectId("62c4d5e11a23db9b29b9fca3"), "user" : "admin1", "password" : "password" }
```

# Delete data in the collection
## Syntax
```C#
//Delete just the first when the parameter is true
db.NameOfCollection.deleteOne({"WHERE"});

//Delete all data from the collection when the parameter is true
db.NameOfCollection.deleteMany({"WHERE"});

//Delete all data from the collection
db.NameOfCollection.deleteMany({});
```
## Example
```C#
//Data
{ "_id" : ObjectId("62c4d3121a23db9b29b9fc9e"), "name" : "Joao", "age" : "10" }
{ "_id" : ObjectId("62c4d3121a23db9b29b9fc9f"), "name" : "Joao", "age" : "20" }
{ "_id" : ObjectId("62c4d3121a23db9b29b9fca0"), "name" : "Lucas", "age" : "15" }

//DELETE the first with name = Joao
db.user.deleteOne({"name" : "Joao"}); 

//Result
{ "_id" : ObjectId("62c4d3121a23db9b29b9fc9f"), "name" : "Joao", "age" : "20" }
{ "_id" : ObjectId("62c4d3121a23db9b29b9fca0"), "name" : "Lucas", "age" : "15" }

//DELETE all data with name = Joao
db.user.deleteMany({"name" : "Joao"}); 

//Result
{ "_id" : ObjectId("62c4d3121a23db9b29b9fca0"), "name" : "Lucas", "age" : "15" }
```

# Delete database
## Syntax
```C#
//Select the database
use database;
//Delete it
db.dropDatabase();
```