Hello,
While trying to run the following query, it is working fine on mongo shell, but I have the following on my golang application:
Invalid $set :: caused by :: Use of undefined variable: this
db.getCollection('interests').aggregate([
{
$lookup:{
from: "products",
localField: "product_types.product_id",
foreignField: "_id",
as: "productInterestData"
}
},
{
$set: {
"product_types": {
$map: {
input: "$product_types",
in: {
$mergeObjects: [
"$$this",
{ product: {
$arrayElemAt: [
"$productInterestData",
{$indexOfArray: ["$productInterestData._id", "$$this.product_id"]}
]
}}
]
}
}
}
}
}
])
pipeline := mongo.Pipeline{
// join product model
// lookup field
bson.D{bson.E{Key: "$lookup", Value: bson.M{
"from": "products",
"localField": "product_types.product_id",
"foreignField": "_id",
"as": "productInterestData",
}}},
// set array
bson.D{bson.E{Key: "$set", Value: bson.E{
Key: "product_types", Value: bson.E{
Key: "$map", Value: bson.D{
bson.E{Key: "input", Value: "$product_types"},
bson.E{Key: "in", Value: bson.E{
Key: "$mergeObjects", Value: bson.A{
"$$this",
bson.E{Key: "product", Value: bson.E{Key: "$arrayElemAt", Value: bson.A{
"$productInterestData",
bson.E{Key: "$indexOfArray", Value: bson.A{"$productInterestData._id", "$$this.product_id"}},
}}},
},
}},
},
},
}}},
// join data
bson.D{bson.E{Key: "$unset", Value: "productInterestData"}},
}
cursor, err := ctx.Store.GetCollection(ctx, &models.Interest{}).Aggregate(c, pipeline)
if err != nil {
utils.Log(c, "warn", err)
return
}