-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_collections_and_rowdata_create.txt
100 lines (82 loc) · 1.21 KB
/
add_collections_and_rowdata_create.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
==========================
db.categories.insert([
{
_id:'c1',
name:"sci-f1"
},
{_id:'c2',
name:'romance'
}
])
db.books.insert([
{
_id:'b1',
name:"Groovy Book",
category:"c1",
authors:['a1']
},
{
_id:'b2',
name:"JAVa Book",
category:"c2",
authors:['a1','a2']
}
])
db.lendings.insert([
{
_id:'11',
book:'b1',
date: new Date('01/01/11'),
lendingBy:'jose'
},
{
_id:'12',
book:'b1',
date: new Date('02/02/12'),
lendingBy:'maria'
}
]);
db.authors.insert([
{
_id:'a1',
name:{first:'orlando',last:'becerra'},
age: 27
},
{
_id:'a2',
name:{first:'mayra',last:'sanchez'},
age: 21
}
]);
db.books.find().forEach(
function (newBook){
newBook.category =db.categories.findOne({"_id":newBook.category});
newBook.lendings =db.lendings.find({"book":newBook._id}).toArray();
newBook.authors = db.authors.find({"_id":{$in:newBook.authors}}).toArray();
db.booksReloaded.insert(newBook);
}
);
db.booksReloaded.find().pretty()
================
db.leftCollection.aggregate(
[{
$lookUp:
{
from:"rightCollection",
localField:"leftVal",
foreignField: "rightVal",
as:"embeddedData"
}
}]
)
db.inventory.aggregate(
[{
"$lookUp":
{
from:"orders",
localField:"sku",
foreignField: "item",
as:"embeddedData"
}
}]
)