I have the following structure in my MongoDB database for a product :
product_1 = {
'name':'...',
'logo':'...',
'nutrition':'...',
'brand':{
'name':'...',
'logo':'...'
},
'stores':{[
'store_id':...,
'url':'...',
'prices':{[
'date':...,
'price':...
]}
]}
})
My pymongo
script goes from store to store and I try to do the following thing :
- if the product is not in the database : add all the informations about the product with the current price and date for the current store_id.
- if the product is in the database but I don't have any entries for the current stroe : add an entry in
stores
with the current price, date and store_id. - if the product is in the database and I have a price entry for the current shop but the current price is not the same : add a new entry with the new date and price for the current store_id.
Is it possible to do all in one request ? For now I have been trying the following, without really knowing how to handles the stores
and prices
case.
Maybe it is not the best way to contruct my database, I am open to suggestions.
db.find_and_modify(
query={'$and':[
{'name':product['name']},
{'stores':{'$in':product['store_id']}}
]},
update={
'$setOnInsert':{
'name':product['product_name'],
'logo':product['product_logo'],
'brand':product['brand'],
[something for stores and prices ?]
},
},
upsert=True
)