Go Mongo driver Use of undefined variable: this

Viewed 107

While trying to run the following query, which is working fine on mongo shell, I have the following error on my golang application:

Invalid $set :: caused by :: Use of undefined variable: this

It looks like there is a problem with the use of $$this

Mongo Shell query:

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"]}
                  ]
                }}
            ]
          }
        }
      }
    }
  }
])

Golang code (implementation of mongo query):

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
        }
0 Answers
Related