-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb cli commands
136 lines (108 loc) · 3.95 KB
/
mongodb cli commands
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
Text Indexing
db.kitchen.insertMany([
... {_id: 1, dept: "tech", description: "lime green computer" },
... {_id: 2, dept: "tech", description: "wireless red mouse" },
... {_id: 3, dept: "kitchen", description: "green placemat" },
... {_id: 4, dept: "kitchen", description: "red peeler" },
... {_id: 5, dept: "food", description: "green apple" },
... {_id: 6, dept: "food", description: "red potato" }])
Creating Index
db.kitchen.createIndex({dept:1, description: "text"})
dept_1_description_text
Querying through the kitchen collection
db.kitchen.find({dept: "kitchen", $text: {$search: "green"}})
[ { _id: 3, dept: 'kitchen', description: 'green placemat' } ]
Excercise 10 on mongosh
Create a collection called "articles" and insert documents representing articles with fields like "title" and "content".
Create a text index on the "content" field.
Write a query to search for articles that contain a specific keyword in their content.
db.articles.insertMany([
... {
... 'title': 'Introduction to MongoDB',
... 'content': 'MongoDB is a NoSQL database...',
... },
... {
... 'title': 'Python Programming Guide',
... 'content': 'Python is a powerful programming language...',
... },
... {
... 'title': 'Web Development Basics',
... 'content': 'Web development involves creating...',
... }])
db.articles.createIndex({content: "text"})
content_text
db.articles.find({$text:{$search:"python"}})
[
{
_id: ObjectId("64903d3389c9e74320c47ca1"),
title: 'Python Programming Guide',
content: 'Python is a powerful programming language...'
}
]
2DSphere indexing
Create a collection called "restaurants" and insert documents representing restaurants with fields like "name", "location", and "cuisine".
Create a 2dsphere index on the "location" field.
Write a query to find all restaurants within a certain distance from a given latitude and longitude.
db.places.insertMany( [
... {
... loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
... name: "Central Park",
... category : "Parks"
... },
... {
... loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
... name: "La Guardia Airport",
... category : "Airport"
... }
... ] )
2Dsphere indexing
db.places.createIndex({loc:'2dsphere'})
Querying a 2dsphere
db.places.find({loc:{$near:{$geometry:{type:'Point',coordinates: [ -73.97, 40.77 ]},$maxDistance: 5000}}})
result
[
{
_id: ObjectId("649040f589c9e74320c47ca3"),
loc: { type: 'Point', coordinates: [ -73.97, 40.77 ] },
name: 'Central Park',
category: 'Parks'
}
]
Grouping and aggregate question
Given a collection of documents representing students, each containing fields like "name", "age", and "grade",
write a MongoDB query to calculate the average age and average grade for each grade level.
The inserted collection can be found in the python file of this question
db.students.aggregate([{ $group:{"_id":"$grade","averageAge":{$avg: "$age"}, "count":{$sum:1}}},
... {$sort:{"_id":1}}])
[
{ _id: 'A', averageAge: 21, count: 8 },
{ _id: 'B', averageAge: 22, count: 6 },
{ _id: 'C', averageAge: 20, count: 6 }
]
Array Fields
Create a collection called "employees" and insert documents representing employees with fields like "name" and "skills" (an array of skills).
Write a query to find all employees who possess a specific skill.
The inserted collection can be found in the python file of this question
db.employees.find({skills:{$in:['JavaScript']}})
[
{
_id: ObjectId("6490628fb52980403fc6e2dc"),
name: 'Alice',
skills: [ 'Python', 'Django', 'JavaScript' ]
},
{
_id: ObjectId("6490628fb52980403fc6e2dd"),
name: 'Bob',
skills: [ 'JavaScript', 'React', 'Node.js' ]
},
{
_id: ObjectId("6490628fb52980403fc6e2e1"),
name: 'Frank',
skills: [ 'Python', 'Flask', 'JavaScript' ]
},
{
_id: ObjectId("6490628fb52980403fc6e2e2"),
name: 'Grace',
skills: [ 'JavaScript', 'Angular', 'Node.js' ]
}
]