Hi @franck_ishemezwe - Welcome to the community.
Not too sure if you’ve solved this yet but I just wanted to confirm a few items:
- Based off the “data collected”, please confirm the two documents are the ones you wish to add to the
"price_detail"
array:
/// Documents to be pushed / added
{
"fuel_id": 2,
"fuel_name": "Gazole",
"fuel_cost": 1.59,
"update_date": {
"$date": "2022-07-14T00:10:19Z"
}
},
{
"fuel_id": 1,
"fuel_name": "Gazole",
"fuel_cost": 35.87,
"update_date": {
"$date": "2022-05-31T10:09:22Z"
}
}
- Are there multiple documents that are returned using the query portion of your update? I’m curious to see if any of those documents have the
"price_detail"
field as an object rather than type array based off the error you provided. Example of this below:
DB> db.stations2.find()
[
{ _id: ObjectId("630fee2ba0eae719ee140850"), price_detail: { a: 1 } }
]
DB> db.stations2.updateOne({},{$push:{"price_detail":{$each:[{a:2}]}}})
MongoServerError: The field 'price_detail' must be an array but is of type object in document {_id: ObjectId('630fee2ba0eae719ee140850')}
- Driver version in use
- MongoDB version in use
Note: The above examples are performed / shown in mongosh
Additionally, from an initial glance, this type of schema design and work flow may lead to the “price_detail” array growing indefinitely which means that it will most likely encounter issues in future if the application runs long enough due to the fact that it may hit the BSON document size limit. One alternative to this would be perhaps having the "price_detail"
as a collection instead of an array. However, it may not reach this point depending on your environment or use case
Regards,
Jason