forked from NickStefan/mongo-joins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
229 lines (214 loc) · 5.42 KB
/
index.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var async = require('async');
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/mongojoins';
async.auto({
// CONNECT TO A DB
db: function(done){
MongoClient.connect(url, done);
},
// CLEAR ANY OLD STUFF
dbClear: ['db', function(done, results){
results.db.collection('content').drop(function(err, results){
done();
});
}],
// INSERT SOME IMAGES
insertImages: ['dbClear', function(done, results){
results.db.collection('content')
.insertMany([
{
type: 'Image',
name: 'Golden Fields Closeup',
url: 'https://static.pexels.com/photos/59498/pexels-photo-59498.jpeg'
},
{
type: 'Image',
name: 'Green Fields Closeup',
url: 'https://static.pexels.com/photos/34021/pexels-photo.jpg'
},
{
type: 'Image',
name: 'Stump at Sunset',
url: 'https://static.pexels.com/photos/1564/dawn-nature-sunset-dust.jpg'
}
], done);
}],
// INSERT SOME TEXT
insertText: ['insertImages', function(done, results){
results.db.collection('content')
.insertMany([
{
type: 'Text',
text: 'Stock photos look nice.',
},
{
type: 'Text',
text: 'Is there such a thing as stock text?'
},
{
type: 'Text',
text: 'Lorem Ipsum isn\'t really stock text'
}
], done);
}],
// QUERY THE IMAGES FOR THE IDs
queryImages: ['insertText', function(done, results){
results.db.collection('content')
.find({type:'Image'})
.toArray(done);
}],
// QUERY THE TEXT FOR THE IDs
queryText: ['queryImages', function(done, results){
results.db.collection('content')
.find({type: 'Text'})
.toArray(done);
}],
// CREATE A PAGE WITH SOME IMAGE IDs
insertPage: ['queryText', function(done, results){
var root = new ObjectID();
var sectionOne = new ObjectID();
var sectionTwo = new ObjectID();
var sectionOneLeftChild = new ObjectID();
var sectionOneMiddleChild = new ObjectID();
var sectionOneRightChild = new ObjectID();
var sectionTwoLeftChild = new ObjectID();
var sectionTwoMiddleChild = new ObjectID();
var sectionTwoRightChild = new ObjectID();
results.db.collection('content')
.insertOne({
type: 'Page',
nodes: [
{
_id: root,
parent_id: null,
next_id: null,
previous_id: null
},
{
_id: sectionOne,
parent_id: root,
next_id: sectionTwo,
previous_id: null,
view: 'section'
},
{
_id: sectionTwo,
parent_id: root,
next_id: null,
previous_id: sectionOne,
view: 'section'
},
{
_id: sectionOneLeftChild,
previous_id: null,
next_id: sectionOneMiddleChild,
parent_id: sectionOne,
view: 'image',
props: {
image_id: results.queryImages[0]._id
}
},
{
_id: sectionOneMiddleChild,
previous_id: sectionOneLeftChild,
next_id: sectionOneRightChild,
parent_id: sectionOne,
view: 'image',
props: {
image_id: results.queryImages[1]._id
}
},
{
_id: sectionOneRightChild,
previous_id: sectionOneMiddleChild,
next_id: null,
parent_id: sectionOne,
view: 'image',
props: {
image_id: results.queryImages[2]._id
}
},
{
_id: sectionTwoLeftChild,
previous_id: null,
next_id: sectionTwoMiddleChild,
parent_id: sectionTwo,
view: 'text',
props: {
text_id: results.queryText[0]._id
}
},
{
_id: sectionTwoMiddleChild,
previous_id: sectionTwoLeftChild,
next_id: sectionTwoRightChild,
parent_id: sectionTwo,
view: 'text',
props: {
text_id: results.queryText[1]._id
}
},
{
_id: sectionTwoRightChild,
previous_id: sectionTwoMiddleChild,
next_id: null,
parent_id: sectionTwo,
view: 'text',
props: {
text_id: results.queryText[2]._id
}
}
]
}, done);
}],
// QUERY A PAGE WITH POPULATED IMAGES
// IN ONE QUERY!
query: ['insertPage', function(done, results){
results.db.collection('content')
.aggregate([
{
'$match': {type: 'Page'}
},
{
'$unwind': '$nodes'
},
{
'$lookup':{
from: 'content',
localField: 'nodes.props.image_id',
foreignField: '_id',
as: 'nodes.props.image'
}
},
{
'$lookup':{
from: 'content',
localField: 'nodes.props.text_id',
foreignField: '_id',
as: 'nodes.props.text'
}
},
// {
// '$unwind': '$nodes.props.image'
// },
// {
// '$unwind': '$nodes.props.text'
// },
{
'$group': {
_id: '$_id',
nodes: {
'$push': '$nodes'
}
}
}
], done);
}]
}, function(err, results){
if (err){
return console.log(err);
}
console.log(JSON.stringify(results.query, null, 4));
results.db.close();
});