I have an aggregation query (mongodb, using mgo library), which groups the documents by a composite key. I want to add the result of this aggregation to another collection (I have use out stage of aggregation The result of this aggregation, which shows correct results but I cannot use out since it overwrites records every time query is run).
snippet of group stage: basically I am grouping by a key which is composite, of a,b and c.
"$group": bson.M{
"_id": bson.M{
"a": "$a",
"b": "$b",
"c": "$c",
},
x: "x",
y: "y" ..etc
I have tested the aggregation / grouping etc with out stage and it gives expected result.
To add to another collection, I want to read each bson object as a struct and then insert into another collection. The problem is how to define the object for the composite key.
For example:
type test struct {
Id string `bson:"_id"` <---- how to define composite key that I want to use as key in new collection
X string `bson:x`
Y string `bson:y`
}
I hope my question is clear, I can add more information if required to answer this. I am new to golang / bson / mongo world, this is something simple which I am not able to figure out, please help.
Thanks
Figured it out, answering to help if someone runs into the same.
You can created another struct for composite key.
type key struct {
a string
b string
c int
}
type test struct {
Id key `bson:"_id"` <---- how to define composite key that I want to use as key in new collection
X string `bson:x`
Y string `bson:y`
}