Hi, I’m using go mongodb driver for my app but I can’t use push and arrayElemAt in one query.
I have this document:
_id: "123"
records: [
{ status: "created" },
]
price: 10
price_table: [
{
price: 10
},
{
price: 20
}
],
updated_at: (timestamp)
I want to add a new entry to record array, set the price based on an entry in price table, and update updated_at.
I have tried:
err := collection.FindOneAndUpdate(
ctx,
bson.M{ "_id": req.ID},
[]interface{}{
bson.M{
bson.M{
"$push": bson.M{
"records": domain.StatusHistory{
Status: "in_progress",
},
},
},
bson.M{
"$set": bson.M{
"price": bson.M{
"$arrayElemAt": bson.A{"$price_table.price", 1},
},
"updated_at": time.Now(),
},
},
},
&opt,
)
but it returns the error “Unrecognized pipeline stage name: ‘$push’”
I have also not used pipeline but it also has the error “The dollar prefixed field arrayElemAt is not valid for storage”
Please advise if I can make it to 1 query or if 2 query is unavoidable.
Thanks