-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.txt
64 lines (39 loc) · 1.43 KB
/
commands.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
-- import --
mongoimport -d db -c credits --type csv --file credits.csv --headerline
mongoimport -d db -c movies --type csv --file movies.csv --headerline
-One to few Design
-Actors and the movies they've starred in
{
_id: <ObjectId>,
actor_id: <actor_id>,
name: <actor_name>,
movies: [
{<movie>},
{<movie>},
{<movie>}...
]
}
-Validators / Constraints
I did not implement validators.
If I did implement them I would put an type integer validator on actor_id and a string validator on name.
If it is possible I would set a size constraint on name.
/*
QUERIES
*/
-- All documents --
db.actors.find().pretty()
-- Embedded array data based on selected criteria (name) / Projection --
-- Get actor movies array --
db.actors.find({"name":'Jennifer Lawrence'}, {"_id":0, "actor_id":0 , "name":0}).pretty()
-- Sorted output --
-- Sort all actors alphabetically --
db.actors.find({}, {"_id":0, "actor_id":0, "movies":0}).sort({"name": 1}).limit(50)
-- Aggregation --
-- Number of movies actor has been in --
db.actors.aggregate([{$match: {'name': 'Samuel L. Jackson'}} , {$project : {movie_count: {$size: "$movies"}}}])
-- Update --
-- Add new movie to actors movies --
-- Find new movie which you want to add to actors movies
newMovie = db.movies.findOne()
-- Add newMovie to actors (Sam Worthington in this example) movie embedded array
db.actors.update({"name": "Sam Worthington"}, { $push: {"movies": { newMovie }}})