Bento
Posted on July 6, 2022
Summary
- Select/create database
- Insert/create collection/table with data
- Show created databases
- Show all collections in the database
- Select data in the collection
- Update data from one registry/document
- Update all data from one registry/document
- Delete data in the collection
- Delete database
Select/create database
use firstDatabase;
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"}
]);
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"}
]);
Show created databases
Only show the database if something was inserted in some collection
Syntax
show dbs;
Example
//Result example:
admin 0.000GB
config 0.000GB
local 0.000GB
firstDatabase 0.000GB
Show all collections in the database
Syntax
show collections;
Example
//Result example:
person
user
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();
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"
}
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"});
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"}
]);
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"});
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" }
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({});
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" }
Delete database
Syntax
//Select the database
use database;
//Delete it
db.dropDatabase();
💖 💪 🙅 🚩
Bento
Posted on July 6, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.