I am writing generic code which will do both add and update as an atomic operation therefore used findOneAndUpdate. My flatten data with array is inserted as object instead of array. Following are details.
db.test.findOneAndUpdate({"saleId" : "7d55acf0-5bc2-11e8-b3e5-c51df4c55083"},{$set:{"price.priceTier.0.unitSold": 1,}}, {upsert:true})
db.test.find()
{ "_id" : ObjectId("5b0173fbcd90c934727269ac"), "saleId" : "7d55acf0-5bc2-11e8-b3e5-c51df4c55083", "price" : { "priceTier" : { "0" : { "unitSold" : 1 } } } }
How can I make sure that {$set:{"price.priceTier.0.unitSold": 1,}} should add as an array as below?
{ "_id" : ObjectId("5b0173fbcd90c934727269ac"), "saleId" : "7d55acf0-5bc2-11e8-b3e5-c51df4c55083", "price" : { "priceTier" : [{ "unitSold" : 1 } ]} }
Since I am trying to write generic code and using flat npm module to flatten my input data to convert into mongodb query therefore I am trying not to change my query if possible.
Please help.