-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sesion_6.py
91 lines (88 loc) · 2.11 KB
/
Sesion_6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# RETO_5.3
from pymongo import MongoClient
client = MongoClient('mongodb+srv://bedu:1452@cluster0.6x4ut.mongodb.net/test?authSource=admin&replicaSet=atlas-12mrpb-shard-0&connectTimeoutMS=600000&socketTimeoutMS=6000000&readPreference=primary&appname=MongoDB%20Compass&ssl=true')
result = client['sample_airbnb']['listingsAndReviews'].aggregate([
{
'$match': {
'amenities': {
'$in': [
re.compile(r"Ethernet(?i)"), re.compile(r"Wifi(?i)")
]
}
}
}, {
'$count': 'id'
}
])
# RETO 6.1
result = client['sample_airbnb']['listingsAndReviews'].aggregate([
{
'$match': {
'property_type': 'House',
'bedrooms': {
'$gte': 1
}
}
}, {
'$addFields': {
'costo_por_habitacion': {
'$divide': [
'$price', '$bedrooms'
]
}
}
}, {
'$group': {
'_id': '$address.country',
'habitaciones': {
'$sum': 1
},
'precio_total': {
'$sum': '$costo_por_habitacion'
}
}
}, {
'$addFields': {
'precio_promedio': {
'$divide': [
'$precio_total', '$habitaciones'
]
}
}
}, {
'$project': {
'id': 1,
'precio_promedio': 1
}
}
])
# RETO 6.2
result = client['sample_mflix']['comments'].aggregate([
{
'$lookup': {
'from': 'users',
'localField': 'email',
'foreignField': 'email',
'as': 'datos_usuario'
}
}, {
'$addFields': {
'usuario': {
'$arrayElemAt': [
'$datos_usuario', 0
]
}
}
}, {
'$addFields': {
'password': '$usuario.password'
}
}, {
'$project': {
'name': 1,
'_id': 0,
'email': 1,
'password': 1
}
}
])