-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample2.txt
92 lines (84 loc) · 2.8 KB
/
sample2.txt
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
Note: Your answer should not contain any MongoDB query or any unnecessary information.
Example 1:
User's Question: How do I find all users who are older than 30?
MongoDB Query :
db.user.find([
{
"$match": {
"age": { "$gt": 30 }
}
},
{
"$count": 50
}
])
Output: There are 50 users older than 30.
Example 2:
User's Question: What can I have fast and tasty?
MongoDB Query:
db.restaurants.aggregate([
{ "$match": { "name": "John Doe" } },
{ "$unwind": "$addresses" },
{
"$lookup": {
"from": "restaurants",
"let": { "userLocation": "$addresses.googlePin" },
"pipeline": [
{
"$addFields": {
"distance": {
"$sqrt": {
"$sum": [
{ "$pow": [{ "$subtract": [{ "$arrayElemAt": ["$googlePin.coordinates", 0] }, { "$arrayElemAt": [{ "$map": { "input": { "$split": ["$$userLocation", ","] }, "as": "coord", "in": { "$convert": { "input": "$$coord", "to": "double" } } } }, 0] }] }, 2] },
{ "$pow": [{ "$subtract": [{ "$arrayElemAt": ["$googlePin.coordinates", 1] }, { "$arrayElemAt": [{ "$map": { "input": { "$split": ["$$userLocation", ","] }, "as": "coord", "in": { "$convert": { "input": "$$coord", "to": "double" } } } }, 1] }] }, 2] }
]
}
}
}
},
{ "$sort": { "distance": 1 } },
{ "$limit": 1 }
],
"as": "nearestRestaurant"
}
},
{ "$unwind": "$nearestRestaurant" },
{ "$unwind": "$nearestRestaurant.menu" },
{
"$group": {
"_id": "$_id",
"nearestRestaurant": { "$first": "$nearestRestaurant" },
"suggestedDishes": { "$push": "$nearestRestaurant.menu" }
}
},
{
"$project": {
"nearestRestaurant": 1,
"suggestedDishes": { "$slice": ["$suggestedDishes", 4] }
}
}
])
Output: You can have the following fast and tasty dishes from the nearest restaurant: Pizza, Burger, Pasta.
Example 3:
User's Question: User's detail to save?
MongoDB Query:
db.user.insertOne([{
"insertOne": {
"document": {
"name": "Ramji",
"dob": "1990-05-15",
"email": "ramji.doe@example.com",
"addresses": [
{"address": "123 Work Street, Office Complex", "tag": "office"},
{"address": "456 Home Avenue, Residential Area", "tag": "home"}
],
"foodChoices": "Non-Veg",
"foodPreferences": ["Chinese", "Italian"],
"healthConditions": ["Diabetes"],
"nonVegDays": ["Monday", "Thursday"]
}
}
}
])
Output:
Successfully saved the detail.