-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickstart-mongo.sh
54 lines (46 loc) · 1015 Bytes
/
quickstart-mongo.sh
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
# connect to a mongo database (todo)
show databases
use databaseX
# equivalent of count(*)
db.collectionX.countDocuments()
# equivalent of select * from dbX
db.goals.find().limit(10)
# equivalent of
# `select owner, previous
# from goal
# where name = 'goal-5' and owner = 'user1'
# offset 100
# limit next 10 rows only`
db.goals.find({name : 'goal-5',owner : 'user1'}, {owner: 1, previous : 1}).skip(100).limit(10)
# example of join
# equivalent of `SELECT
# c1.col1,
# c1.col2,
# c2.*
# FROM
# collection1 c1
# LEFT JOIN collection2 c2 ON c1.field_in_collection1 = c2.field_in_collection2
# WHERE
# c1.propertyName >= 45;`
db.collection1.aggregate([
{
$lookup: {
from: "collection2",
localField: "field_in_collection1",
foreignField: "field_in_collection2",
as: "joined_data"
}
},
{
$match: {
"propertyName": { $gte: 45 }
}
},
{
$project: {
col1: 1,
col2: 1,
joined_data: 1
}
}
])