Go driver, run aggregation pipeline with $merge


I am new to mongo and was looking for ways to trigger the on-demand-materialised view in Go driver.

I tried running the aggregation pipeline in Go with $merge. But seems like it’s not performing anything.

collection := client.Database("poc").Collection("po_new_audit")

	matchStg := bson.D{{"$match", bson.D{{"synced", false}}}}
	addFieldStg1 := bson.D{{"$addFields", bson.D{{"audit_type", "purchase_order"}}}}
	lookupStg := bson.D{{"$lookup", bson.D{{"from", "user"}, {"localField", "who.id"}, {"foreignField", "_id"}, {"as", "user_details"}}}}
	unwindStg1 := bson.D{{"$unwind", "$user_details"}}
	addFieldStg2 := bson.D{{"$addFields", bson.D{{"breadcrumbs", bson.D{{"po_id", "$entity_id"}}}}}}
	projectStg := bson.D{{"$project", bson.D{{"_id", "1"}, {"what", "1"}, {"when", "1"}, {"user_details", "1"}, {"breadcrumbs", "1"}, {"audit_type", "1"}}}}
	mergeStg := bson.D{{"$merge", bson.D{{"into", "audit"}}}}
	c, err := collection.Aggregate(context.TODO(), mongo.Pipeline{matchStg, addFieldStg1, lookupStg, unwindStg1, addFieldStg2, projectStg, mergeStg})
	if err != nil {

	var loaded []bson.M
	if err = c.All(context.TODO(), &loaded); err != nil {

The aggregation works till the projectStg and cursor gives the return values. But adding $merge doesn’t give an error but returns empty cursor. Also, the merge document doesn’t get updated.

Any pointers will really help.


This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.