Issue with $push operator

Thanks for providing the details requested @franck_ishemezwe

Upon further inspection, it may be possible the error that’s being returned is due to the upsert behaviour as mentioned in the Upsert with Operator Expressions documetation:

If no document matches the query criteria and the <update> parameter is a document with update operator expressions, then the operation creates a base document from the equality clauses in the <query> parameter and applies the expressions from the <update> parameter.

I believe due to the query criteria, the "price_detail" field is of type object rather than an array when it is created as part of the “base document” which will then cause the error when the $push operator is applied.

If it suits your use case, would it be possible for you to utilise $elemMatch at the query portion? The below example is performed via mongosh:

DB>db.collection.find()
/// Empty collection to start
DB> db.collection.updateOne({"price_detail.fuel_id":1},{$push:{"price_detail":{"$each":[1,2,3]}}},{upsert:true})
MongoServerError: The field 'price_detail' must be an array but is of type object in document {no id}
/// Same error returned. Note the query value.
DB> db.collection.updateOne({"price_detail":{$elemMatch:{"fuel_id":1}}},{$push:{"price_detail":{"$each":[1,2,3]}}},{upsert:true})
/// Using $elemMatch instead
{
  acknowledged: true,
  insertedId: ObjectId("6317d956c3d5b1b653dc09bf"),
  matchedCount: 0,
  modifiedCount: 0,
  upsertedCount: 1
}
DB> db.collection.find()
[
  {
    _id: ObjectId("6317d956c3d5b1b653dc09bf"),
    price_detail: [ 1, 2, 3 ]
  }
]

You can alter your current update_one() method accordingly with $elemMatch although this would only be if it suits your use case as I understand your current <query> within the update operation is checking for if the "price_detail.fuel_name" and "price_detail.fuel_id" separately regardless if they exist in the same object within the array. It is highly recommended to test thoroughly on a test environment after making the required changes to verify it suits all your use case(s) and requirements.

Regards,
Jason

1 Like