DEV Community

Bento
Bento

Posted on

Basic commands MongoDB

Summary

  1. Select/create database
  2. Insert/create collection/table with data
  3. Show created databases
  4. Show all collections in the database
  5. Select data in the collection
  6. Update data from one registry/document
  7. Update all data from one registry/document
  8. Delete data in the collection
  9. Delete database

Select/create database

use firstDatabase;
Enter fullscreen mode Exit fullscreen mode

Insert/create collection/table with data

Syntax

//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"}
]);
Enter fullscreen mode Exit fullscreen mode

Example

//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"}
]);

Enter fullscreen mode Exit fullscreen mode

Show created databases

Only show the database if something was inserted in some collection

Syntax

show dbs;
Enter fullscreen mode Exit fullscreen mode

Example

//Result example:
admin    0.000GB
config   0.000GB
local    0.000GB
firstDatabase  0.000GB
Enter fullscreen mode Exit fullscreen mode

Show all collections in the database

Syntax

show collections;
Enter fullscreen mode Exit fullscreen mode

Example

//Result example:
person
user
Enter fullscreen mode Exit fullscreen mode

Select data in the collection

Syntax

//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(); 
Enter fullscreen mode Exit fullscreen mode

Example

//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"
}
Enter fullscreen mode Exit fullscreen mode

Update data from one registry/document

Syntax

/*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"});
Enter fullscreen mode Exit fullscreen mode

Example

//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"}
]);
Enter fullscreen mode Exit fullscreen mode

Update all data from one registry/document

Syntax

/*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"});
Enter fullscreen mode Exit fullscreen mode

replaceMany

Don't exist

Example

//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" }
Enter fullscreen mode Exit fullscreen mode

Delete data in the collection

Syntax

//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({});
Enter fullscreen mode Exit fullscreen mode

Example

//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" }
Enter fullscreen mode Exit fullscreen mode

Delete database

Syntax

//Select the database
use database;
//Delete it
db.dropDatabase();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)