I need to find duplicates with aggregation and lookup with MongoDB and Golang.
Here is my Event
structure.
// Event describes the model of an Event
type Event struct {
ID string `bson:"_id" json:"_id" valid:"alphanum,printableascii"`
OldID string `bson:"old_id" json:"old_id" valid:"alphanum,printableascii"`
ParentID string `bson:"_parent_id" json:"_parent_id" valid:"alphanum,printableascii"`
Name string `bson:"name" json:"name"`
Content string `bson:"content" json:"content"`
Slug string `bson:"slug" json:"slug"`
LocationID string `bson:"_location_id" json:"_location_id"`
Price string `bson:"price" json:"price"`
CreatedBy string `bson:"created_by" json:"created_by"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
ModifiedAt time.Time `bson:"modified_at" json:"modified_at"`
}
Here is the request I already have :
// Create the pipeline
pipeline := []bson.M{
bson.M{
"$group": bson.M{
"_id": bson.M{
"_location_id": "$_location_id",
"start_date": "$start_date",
},
"docs": bson.M{"$push": "$_id"},
"count": bson.M{"$sum": 1},
},
},
bson.M{
"$match": bson.M{
"count": bson.M{"$gt": 1.0},
},
},
}
// Do the request
dupes := []bson.M{}
err := session.DB(shared.DatabaseNamespace).C(dao.collection).Pipe(pipeline).All(&dupes)
The events must not have the same start_date and the same _location_id. This is what I can get :
/* 1 */
{
"_id" : {
"_location_id" : "4okPZllaoueYC3U2",
"start_date" : ISODate("2018-04-22T18:00:00.000Z")
},
"count" : 2.0,
"docs" : [
"FFSC2sJcrWgj2FsU",
"lwHknTHFfVAzB8ui"
]
}
/* 2 */
{
"_id" : {
"_location_id" : "pC8rlLVao5c2CeBh",
"start_date" : ISODate("2018-04-03T19:00:00.000Z")
},
"count" : 2.0,
"docs" : [
"jPRbkINiCExzh2tT",
"C8hx92QSZEl7HUIz"
]
}
Fine, it is working, but..
I would like to obtain, directly from Mongo, an array of my Event type, and if it is possible, an array of array of Event : [][]*Event
.
In order words, an array of the duplicates (between them).
For example :
// Pipeline
...
// Do the request
events := [][]*Events
err := session.DB(shared.DatabaseNamespace).C(dao.collection).Pipe(pipeline).All(&events)
Or, do I need to perform the logic with Golang to achieve what I need ?
The libraries I use are :
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
Note only : no need to take care about the _location_id
, I lookup it with a DTO inside my Golang logic.
EDIT : If I cannot lookup the IDs, can I at least obtain the IDs as an array directly in the result ? For example :
[
"jPRbkINiCExzh2tT",
"C8hx92QSZEl7HUIz"
]
This is what I tried to add to the request : {$out: "uniqueIds"}
. But it is not working.