I'm using python's library for Mongo (pymongo) and my document looks like this:
{"vendor_id": 12, "title": "xyz", "price": 1499.0, "price_history": [{"date": "2019-12-01", "price": 1890.0}]}
I would like to push new price object to the "price_history" array if document with id=12 exists. If it doesn't I would create a new document that look the same as the pasted code.
It seems simple but I've checked multiple stackoverflow topics and mongodb docs and can't get it :/
I've come up with the code:
db.holidays.update_one(
{"vendor_id": t["vendor_id"]},
{"$push": {"price_history": t["price_history"][0]}},
upsert=True
)
but when document isn't found it inserts only vendor_id instead of entire document.
Any tips? Thank you for spending your time on my problem.
vendor_id,title,price, and other fields?$setOnInsertis exactly the way to go.