mongo db Flashcards

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
Q

$pull => one element

A

db.inventory.updateOne(
{ _id: 3 },
{
$pull: {
tags: 100,
},
}
);

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

$pull => a lot of elements

A

db.inventory.updateOne(
{ _id: 3 },
{
$pull: {
tags: {
$in:[“school”,200,300]
},
},
}
);

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

put # update.Many()

A

use(“platzi_store”);

db.inventory.updateMany(
{ },
{
$pus: {
tags: {
$each:[100,200]
},
},
}
);

db.inventory.find();

28
Q

pull # update.Many()

A

use(“platzi_store”);

db.inventory.updateMany(
{ },
{
$pull: {
tags: {
$in:[100,200]
},
},
}
);

db.inventory.find();

29
Q

pop # for all
# very important => you can only enter 1 or -1

A

use(“platzi_store”);

db.inventory.updateMany(
{},

{
$pop: {
tags: 1,
},
}
);

db.inventory.find();

30
Q

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.

A

use(“platzi_store”);

db.iot.updateOne(
{
sensor: “one”,
date: “2022-01-01”,
},
{
$push: {
readings: 100,
},
},
{
upsert: true,
}
);

db.iot.find();

31
Q

equal => $eq
implicit

A
32
Q

no equals => $ne

A

use(“platzi_store”);

db.inventory.find({
qty: {
$ne: 15,
},
});

33
Q

$gt => greater than
$gte => greater than equals
$lt => less than
$lte => less than equals (iquols)

A

use(“platzi_store”);

db.inventory.find({
qty: {
$gt: 10, # gt // gte // lt // lte
},
});

34
Q

both at the same time

A

use(“platzi_store”);

db.inventory.find({
qty: {
$gt: 10, $lt:20
},
});

35
Q

do two questions at the same time

A

example 1

db.inventory.find({
one:”patient,
two:”bard”
});

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

36
Q

$gte with a $pull

A

use(“platzi_store”);

db.iot.updateMany(
{
sensor: “A001”,
},
{
$pull: {
readings: { $gte: 3 },
},
}
);

db.iot.find();

37
Q

$ask in mongo compas => it’s like it’s inside if

A

{tripduration:{$lte:200},usertype:”Subscriber”}

38
Q

$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
A

db.inventory.find({
“item.description”: {
$regex: /line/,

},
});

39
Q

projection

  • 1 to show
  • 0 dont show => only use in id
A

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
Q

$in => is like a ‘or’
$nin

A

$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
Q

arrays: is is enough to have one element of the array #very important

A

for several values

db.inventory.find({
tags: “school”,
});

=>
“tags”: [
“school”,
“book”,
“bag”,
“headphone”,
“appliance”
]

db.inventory.find({
tags: {
$in:[“one”,”two”] }
});

42
Q

what if is:

// It has to be identical, order matters // tiene que ser identica

tags: [“school”],
tags: [“school”, “book”],

A
43
Q

$all
have all the elements

A

db.inventory.find({
tags: {
$all: [“book”, “school”],
},
});

=>
“tags”: [
“school”,
“book”,
“bag”,
“headphone”,
“appliance”
]

44
Q

$ size
array with this number of elements

A

use(“platzi_store”);

db.inventory.find({
tags: {
$size: 2,
},
});

=>
“tags”: [
“school”,
“book”
]

45
Q

$elemMatch => use for ARRAYS with OBJECTS

[
{
id:1,
result: ACHIEVE,
},

{
id:2,
result: ACHIEVE,
},

]

A

example 2

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
}
]
}

db.survey.find({
results: { $elemMatch: { product: “xyz” ,score:{$gte:7}} },
});

$elemMatch: {
“person.first_name”: “Mark”,

46
Q

AND implicit
AND inside ARRAY

one attribute = $in:[10,20]
more attributes = $or

A
47
Q

the first element

A

db.survey.findOne()

48
Q

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
A

db.inventory.find({
tags: {
$in: [“A”, “B”],
},
});

===============================

db.inventory.find({
tags: {
$all: [“A”, “B”],
},
});

49
Q

search as array :)

A

use(“platzi_store”);

db.inventory.find({
“tags.0”: “A”, // “tags.100”: “A”,
});

50
Q

AND implicit

A

db.inspections.find({
sector: “Tax Preparers - 891”,
result: “Unable to Locate”,
})

51
Q

$or

A

db.inspections.find({
$or: [
{ sector: “Tax Preparers - 891” },
{ result: “Unable to Locate” }
],
});

52
Q

$nor

A

db.inspections.find({
$nor: [
{ sector: “Tax Preparers - 891” },
{ result: “Unable to Locate” }
],
});

53
Q

$not

A

use(“sample_training”);

db.inspections.find({
result: {
$not: {
$regex: /unable/i,
},
},
});

54
Q

use $AND & $OR

A

are the same

A ∩ (B U C) => (A ∩ B) U (A ∩ C)

db.routes.find({
$and: [
{ airplane: “E70” },
{ $or: [{ dst_airport: “BOG” }, { dst_airport: “BOG” }] },
],
});

55
Q

$expr

A

example basic

compare colums (very important)
db.monthlyBudget.find({ $expr: { $gt: [“$spent”, “$budget”] } });

db.monthlyBudget.find({ $expr: { $gt: [“$spent”, 100] } });

56
Q

$exp & $eq

A

use(“sample_training”);

db.trips.find({
$expr: {
$eq: [“$start station id”, “$end station id”],
},
});

57
Q

a lot of => $exp

A

db.trips.find({
$expr: {
$eq: [“$start station id”, “$end station id”],
},
$expr: {
$gte: [“$bikeid”, 22000],
},
});

58
Q

db.companies.find({
“relationships.0.person.last_name”: “Zuckerberg”,

},
{});

A

{
“relationships”: [
{ }, // return
{ },
{ }
]

},

{
“relationships”: [
{ }, // return
{ },
{ }
]

},

59
Q

agregation framework

db.AAA.aggregate([
{ $match: { } },
{ $project: { } },
{ $group: { } },
]);

A

data analytics

agregation framework : {
mongo query language
}

60
Q

agregation framework #example

db.listingsAndReviews.find(
{
amenities: “Wifi”,
},
{
price: 1,
amenities: 1,
}
);

A

the same

db.listingsAndReviews.aggregate([
{ $match: { amenities: “Wifi” } },
{ $project: { price: 1, amenities: 1 } },
]);

61
Q

group // agregation framework

A

example 2 // more important

example
db.listingsAndReviews.aggregate([
{ $match: { amenities: “Wifi” } },
{ $project: { address:1} },
{ $group: {_id: “$address.country”, total: {$sum: 1} } }
]);

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
Q

sort

// 1 : ascending
// -1 descending

A

use(“sample_training”);

db.zips
.find({ pop: { $gte: 100 } })
.sort({ pop: 1 });

63
Q

limit

A

example basic

db.zips
.find()
.limit(2);

example with => sort
db.zips
.find({ one: { $gte: 100 } })
.sort({ one: -1})
.limit(2)

64
Q

skip => starts from N

A

db.categories.find()
.skip(3) //

=>
[
{
“_id”: 4,
“name”: “category 4”
},
{
“_id”: 5,
“name”: “category 5”
},
{

65
Q

mongo chart

A

dashboards

66
Q
A