@@ -171,8 +171,6 @@ func UploadData[T any](client *mongo.Client, ctx context.Context, fptr *os.File,
171171
172172// MongoDB aggregation run only when sections changes, instead of as a view where it's run for each query
173173func buildEvents (client * mongo.Client , ctx context.Context ) {
174- sections := getCollection (client , "sections" )
175-
176174 pipeline := mongo.Pipeline {
177175 //separate each meeting
178176 {{Key : "$unwind" , Value : "$meetings" }},
@@ -311,15 +309,28 @@ func buildEvents(client *mongo.Client, ctx context.Context) {
311309 }},
312310 {Key : "buildings" , Value : 1 },
313311 }}},
314- //set as events collection
315- {{Key : "$out" , Value : "events" }},
316312 }
317313
314+ sections := getCollection (client , "sections" )
318315 cursor , err := sections .Aggregate (ctx , pipeline )
319316 if err != nil {
320317 log .Panic (err )
321318 }
322319 defer cursor .Close (ctx )
323320
321+ events := getCollection (client , "events" )
322+ var results []interface {}
323+ if err := cursor .All (ctx , & results ); err != nil {
324+ log .Panic (err )
325+ }
326+
327+ if len (results ) > 0 {
328+ events .DeleteMany (ctx , bson.D {})
329+ _ , err = events .InsertMany (ctx , results )
330+ if err != nil {
331+ log .Panic (err )
332+ }
333+ }
334+
324335 log .Println ("Done aggregating events!" )
325336}
0 commit comments