mongo db Flashcards

(66 cards)

1
Q

how is ??

A

db => collections (sql tables) => documents (objetos)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

mongo db atlas

A

cluster => escalamiento vertical

// search more information

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

mongo query lenguage

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

extension del archivo

A

one.mongodb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

inside the file

A

use(“platzi_store”) # name DB
db.zip.find({state: “NY”}) # name collection
db.zip.find({state: “NY”}).count()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

mongosh

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

insert

A

db.products.insertOne({
name: “Product 2”,
price: 5000,

})

db.products.insertOne({
_id:1,
name: “Product 1”,
price: 100,
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

error

A

si funciona pero solo hasta donde se genere el error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

se ejecuta aun hubiera errores

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to delete

A

use(“platzi_stoire”)
db.products.drop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

always get error

A

even you put => {orderd:false}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

do a query
{
{
}
}

A

use(“platzi_store”);

db.inventory.find({
“item.name”: “ab”,
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

no tiene corchetes globales

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

SET

MODIFY && IMPROVE the same time

A

use(“platzi_store”);

db.procutst2.updateOne(
{ _id: 1 },

{
$set: {
name: “Smartphone 20”,
beatiful: true,
tags: [100, 200, 300],
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

INC

INCREASE

A

use(“platzi_store”);

db.procutst2.updateOne(
{ _id: 1 },

{
$inc: {
price: 1,
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

ObjectId

A

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,
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

updateMany : set

A

db.zips.updateMany(
{ city: “CLEVELAND” },

{
$set: {
see: “looking for the doors of love”,
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

updateMany : inc

A

db.zips.updateMany(
{ city: “CLEVELAND” },

{
$inc: {
pop: 10000000
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

updateMany : rename
#change the column name

A

use(“sample_training”);

db.zips.updateMany(
{ city: “CLEVELAND” },

{
$rename: {
see: “see2”
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

updateMany : unset
#delete a column

A

use(“sample_training”);

db.zips.updateMany(
{ city: “CLEVELAND” },

{
$unset: {
see2: “”,
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

change all elements of a table

A

db.zips.updateMany(
{ }

{
$set: {
see333: “you are selfish ans main”,
},
}
);

db.zips.find()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

$push => one element

A

use(“platzi_store”);

db.inventory.updateOne(
{ _id: 4 },
{
$push: {
tags: “headphone”,
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

$push => two elements

A

use(“platzi_store”);

db.inventory.updateMany(
{ _id: 1 },
{
$push: {
tags: {
$each: [700, 800],
},
},
}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
$pull => one element
db.inventory.updateOne( { _id: 3 }, { $pull: { tags: 100, }, } );
26
$pull => a lot of elements
db.inventory.updateOne( { _id: 3 }, { $pull: { tags: { $in:["school",200,300] }, }, } );
27
put # update.Many()
use("platzi_store"); db.inventory.updateMany( { }, { $pus: { tags: { $each:[100,200] }, }, } ); db.inventory.find();
28
pull # update.Many()
use("platzi_store"); db.inventory.updateMany( { }, { $pull: { tags: { $in:[100,200] }, }, } ); db.inventory.find();
29
pop # for all # very important => you can only enter 1 or -1
use("platzi_store"); db.inventory.updateMany( {}, { $pop: { tags: 1, }, } ); db.inventory.find();
30
#upsert Insert: Adding a new record to the database. If the record already exists, the operation will fail. Upsert: Adding a new record to the database. If the record already exists, it will be updated with the new data instead of failing.
use("platzi_store"); db.iot.updateOne( { sensor: "one", date: "2022-01-01", }, { $push: { readings: 100, }, }, { upsert: true, } ); db.iot.find();
31
equal => $eq implicit
32
no equals => $ne
use("platzi_store"); db.inventory.find({ qty: { $ne: 15, }, });
33
$gt => greater than $gte => greater than equals $lt => less than $lte => less than equals (iquols)
use("platzi_store"); db.inventory.find({ qty: { $gt: 10, # gt // gte // lt // lte }, });
34
both at the same time
use("platzi_store"); db.inventory.find({ qty: { $gt: 10, $lt:20 }, });
35
do two questions at the same time
#example 1 db.inventory.find({ one:"patient, two:"bard" }); #example 2 db.inventory.find({ "item.name": "ab", qty: { $gt: 10 }, });
36
$gte with a $pull
use("platzi_store"); db.iot.updateMany( { sensor: "A001", }, { $pull: { readings: { $gte: 3 }, }, } ); db.iot.find();
37
$ask in mongo compas => it's like it's inside if
{tripduration:{$lte:200},usertype:"Subscriber"}
38
$regex # same a regex regex: /line/ => this word have in the line regex: /LINE/i => ignore uppercas or lowercase regex: /^line/m => the word has to start with // always add m regex: /line$/m => the word has to start with // always add m regex: /^MANY/mi => both at the same time $regex: /^Second/m => doesnt matter: \n
db.inventory.find({ "item.description": { $regex: /line/, }, });
39
projection - 1 to show - 0 dont show => only use in id
example with 1 use("sample_training"); db.trips.find( { tripduration: { $gte: 400 }, usertype: "Subscriber" }, { tripduration: 1, } ); example with 2 use("sample_training"); db.trips.find( { tripduration: { $gte: 400 }, usertype: "Subscriber" }, { tripduration: 1, _id:0 } );
40
$in => is like a 'or' $nin
$in use("platzi_store"); db.inventory.find({ qty: { $in: [20, 25] }, # qty: { $in: ["one","two"] }, }); $nin use("platzi_store"); db.inventory.find({ qty: { $nin: [20, 25] }, });
41
arrays: is is enough to have one element of the array #very important
db.inventory.find({ tags: "school", }); => "tags": [ "school", "book", "bag", "headphone", "appliance" ] #for several values db.inventory.find({ tags: { $in:["one","two"] } });
42
what if is: // It has to be identical, order matters // tiene que ser identica tags: ["school"], tags: ["school", "book"],
43
$all have all the elements
db.inventory.find({ tags: { $all: ["book", "school"], }, }); => "tags": [ "school", "book", "bag", "headphone", "appliance" ]
44
$ size array with this number of elements
use("platzi_store"); db.inventory.find({ tags: { $size: 2, }, }); => "tags": [ "school", "book" ]
45
$elemMatch => use for ARRAYS with OBJECTS [ { id:1, result: ACHIEVE, }, { id:2, result: ACHIEVE, }, ]
db.survey.find({ results: { $elemMatch: { product: "xyz" } }, }); => { "results": [ { "product": "abc", "score": 10 }, { "product": "xyz", "score": 5 } ] }, { "results": [ { "product": "abc", "score": 8 }, { "product": "xyz", "score": 7 } ] } #example 2 db.survey.find({ results: { $elemMatch: { product: "xyz" ,score:{$gte:7}} }, }); #example 3 $elemMatch: { "person.first_name": "Mark",
46
AND implicit AND inside ARRAY one attribute = $in:[10,20] more attributes = $or
47
the first element
db.survey.findOne()
48
between $in: ["A", "B"] contains either "book " or "bag" or both A or B or A U B ===================== $all: ["A", "B"], field contains both "book " and "bag" // have the 2 elements
db.inventory.find({ tags: { $in: ["A", "B"], }, }); =============================== db.inventory.find({ tags: { $all: ["A", "B"], }, });
49
search as array :)
use("platzi_store"); db.inventory.find({ "tags.0": "A", // "tags.100": "A", });
50
AND implicit
db.inspections.find({ sector: "Tax Preparers - 891", result: "Unable to Locate", })
51
$or
db.inspections.find({ $or: [ { sector: "Tax Preparers - 891" }, { result: "Unable to Locate" } ], });
52
$nor
db.inspections.find({ $nor: [ { sector: "Tax Preparers - 891" }, { result: "Unable to Locate" } ], });
53
$not
use("sample_training"); db.inspections.find({ result: { $not: { $regex: /unable/i, }, }, });
54
use $AND & $OR
A ∩ (B U C) => (A ∩ B) U (A ∩ C) #are the same db.routes.find({ $and: [ { airplane: "E70" }, { $or: [{ dst_airport: "BOG" }, { dst_airport: "BOG" }] }, ], });
55
$expr
compare colums (very important) db.monthlyBudget.find({ $expr: { $gt: ["$spent", "$budget"] } }); #example basic db.monthlyBudget.find({ $expr: { $gt: ["$spent", 100] } });
56
$exp & $eq
use("sample_training"); db.trips.find({ $expr: { $eq: ["$start station id", "$end station id"], }, });
57
a lot of => $exp
db.trips.find({ $expr: { $eq: ["$start station id", "$end station id"], }, $expr: { $gte: ["$bikeid", 22000], }, });
58
db.companies.find({ "relationships.0.person.last_name": "Zuckerberg", }, {});
{ "relationships": [ { }, // return { }, { } ] }, { "relationships": [ { }, // return { }, { } ] },
59
agregation framework db.AAA.aggregate([ { $match: { } }, { $project: { } }, { $group: { } }, ]);
data analytics agregation framework : { mongo query language }
60
agregation framework #example db.listingsAndReviews.find( { amenities: "Wifi", }, { price: 1, amenities: 1, } );
#the same db.listingsAndReviews.aggregate([ { $match: { amenities: "Wifi" } }, { $project: { price: 1, amenities: 1 } }, ]);
61
#group // agregation framework
example db.listingsAndReviews.aggregate([ { $match: { amenities: "Wifi" } }, { $project: { address:1} }, { $group: {_id: "$address.country", total: {$sum: 1} } } ]); #example 2 // more important db.listingsAndReviews.aggregate([ { $project: { address:1} }, { $group: {_id: "$address.country", total: {$sum: 1} } } ]); => [ { "_id": "Portugal", "total": 555 }, { "_id": "United States", "total": 1222 }, { "_id": "Canada", "total": 649 },
62
sort // 1 : ascending // -1 descending
use("sample_training"); db.zips .find({ pop: { $gte: 100 } }) .sort({ pop: 1 });
63
limit
#example basic db.zips .find() .limit(2); example with => sort db.zips .find({ one: { $gte: 100 } }) .sort({ one: -1}) .limit(2)
64
skip => starts from N
db.categories.find() .skip(3) // => [ { "_id": 4, "name": "category 4" }, { "_id": 5, "name": "category 5" }, {
65
mongo chart
dashboards
66