mongo db Flashcards
how is ??
db => collections (sql tables) => documents (objetos)
mongo db atlas
cluster => escalamiento vertical
// search more information
mongo query lenguage
extension del archivo
one.mongodb
find
db(“name of DB”) # si no esta creado lo crea
db.products.find() #retur all # si no esta creado lo crea
db.products.find({price:100}) #return
db.products.findOne({price:100}) #return
inside the file
use(“platzi_store”) # name DB
db.zip.find({state: “NY”}) # name collection
db.zip.find({state: “NY”}).count()
mongosh
inside
docker-compose exec mongodb bash
mongosh “mongodb://root:root123@localhost:27017/?tls=false”
show dbs // show collections
use(“platzi_store”) # name DB
db.name_collection.find({state: “NY”}) # name collection
insert
db.products.insertOne({
name: “Product 2”,
price: 5000,
})
db.products.insertOne({
_id:1,
name: “Product 1”,
price: 100,
})
error
si funciona pero solo hasta donde se genere el error
se ejecuta aun hubiera errores
db.products.drop()
db.products.insertMany([{},{}])
para que inserte todos menos los que tienenen error
db.products.insertMany([{},{},
]
,{orderd:false} #fuera del corchete jajajajaja
)
how to delete
use(“platzi_stoire”)
db.products.drop()
always get error
even you put => {orderd:false}
do a query
{
{
}
}
use(“platzi_store”);
db.inventory.find({
“item.name”: “ab”,
});
no tiene corchetes globales
SET
MODIFY && IMPROVE the same time
use(“platzi_store”);
db.procutst2.updateOne(
{ _id: 1 },
{
$set: {
name: “Smartphone 20”,
beatiful: true,
tags: [100, 200, 300],
},
}
);
INC
INCREASE
use(“platzi_store”);
db.procutst2.updateOne(
{ _id: 1 },
{
$inc: {
price: 1,
},
}
);
ObjectId
example with GET
db.procutst2.updateOne(
{ _id: ObjectId(“66132fbe”) },
{
$get: {
price: 800,
},
}
);
other example with INC
db.procutst2.updateOne(
{ _id: ObjectId(“66132fbe”) },
{
$inc: {
price: 1,
},
}
);
updateMany : set
db.zips.updateMany(
{ city: “CLEVELAND” },
{
$set: {
see: “looking for the doors of love”,
},
}
);
updateMany : inc
db.zips.updateMany(
{ city: “CLEVELAND” },
{
$inc: {
pop: 10000000
},
}
);
updateMany : rename
#change the column name
use(“sample_training”);
db.zips.updateMany(
{ city: “CLEVELAND” },
{
$rename: {
see: “see2”
},
}
);
updateMany : unset
#delete a column
use(“sample_training”);
db.zips.updateMany(
{ city: “CLEVELAND” },
{
$unset: {
see2: “”,
},
}
);
change all elements of a table
db.zips.updateMany(
{ }
{
$set: {
see333: “you are selfish ans main”,
},
}
);
db.zips.find()
$push => one element
use(“platzi_store”);
db.inventory.updateOne(
{ _id: 4 },
{
$push: {
tags: “headphone”,
},
}
);
$push => two elements
use(“platzi_store”);
db.inventory.updateMany(
{ _id: 1 },
{
$push: {
tags: {
$each: [700, 800],
},
},
}
);
$pull => one element
db.inventory.updateOne(
{ _id: 3 },
{
$pull: {
tags: 100,
},
}
);
$pull => a lot of elements
db.inventory.updateOne(
{ _id: 3 },
{
$pull: {
tags: {
$in:[“school”,200,300]
},
},
}
);