-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_queries.txt
142 lines (127 loc) · 2.75 KB
/
mongo_queries.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
mongodump -h ds133348.mlab.com:33348/heroku_x4cvh20w -u login -p pass -o d:\ --gzip
db.createCollection("authors");
db.createCollection("tags");
db.createCollection("articles");
db.authors.findOneAndDelete({"name": /^Mikita.*/})
db.articles.insertMany([
{
"title": "Title 1",
"created_at": new Date(),
"updated_at": new Date(),
"author": "supernickname1",
"tags": [2],
"likes": 0,
"article": "it's reaaly big text 1",
"comments": [
{
"first_name": "John",
"last_name": "Round",
"message": "very cool"
}
]
},
{
"title": "Title 2",
"created_at": new Date("2016-12-06"),
"updated_at": new Date("2016-12-06"),
"author": "supernickname2",
"tags": [1, 2, 3],
"likes": 145,
"article": "it's reaaly big text 2",
"comments": [
{
"first_name": "Bon",
"last_name": "Senior",
"message": "very very cool"
},
{
"first_name": "Bonana",
"last_name": "Mamba",
"message": "mmm, tasty!"
}
]
},
{
"title": "Title 3",
"created_at": new Date("2016-02-15"),
"updated_at": new Date("2016-09-28"),
"author": "supernickname3",
"tags": [2],
"likes": 1568,
"article": "it's reaaly big text 3",
"comments": [
{
"first_name": "Jonatan",
"last_name": "Bound",
"message": "very cool, very"
},
{
"first_name": "Mark",
"last_name": "Bounce",
"message": "very very"
},
{
"first_name": "Yuri",
"last_name": "Haharin",
"message": "Let's go"
}
]
}
])
db.tags.insertMany([
{
"_id": 1,
"name": "tag 1"
},
{
"_id": 2,
"name": "tag 2"
},
{
"_id": 3,
"name": "tag 3"
}
])
db.articles.aggregate([
{
$unwind: "$tags"
}
])
db.articles.aggregate([
{
$unwind: "$tags"
},
{
$lookup:
{
from: "tags",
localField: "tags",
foreignField: "_id",
as: "tag_descriptions"
}
},
{
$match: {tag_descriptions: {$elemMatch: {name: "tag 3"}}}
}
])
db.articles.deleteMany({likes: {$lt: 100}});
db.articles.createIndex({likes: 1});
db.tags.createIndex({name: "text"});
mongoimport -h ds133348.mlab.com:33348/heroku_x4cvh20w -c grades -u login -p pass --file grades.json
db.grades.aggregate([
{$unwind: "$scores"},
{$match: {"scores.type": {$ne: "quiz"}}},
{$group: {
_id: {
student_id: "$student_id",
class_id: "$class_id"
},
student_avg: {$avg: "$scores.score"}
}},
{$group: {
_id: "$_id.class_id",
class_avg: {$avg: "$student_avg"}
}},
{$sort: {"class_avg": -1}},
{$limit: 1}
])