So for hours I have been trying to figure out how to add a object if a value is not present in the database or vice versa.
A quick look of a script I have done:
#email_raffleItem = email= "Hello@gmail.com", raffleItem = "hello"
user_email = db.license.find_one({'email': email_raffleItem.email})
if user_email and user_email.get('raffleItem').get(email_raffleItem.raffleItem).get('entries'):
db.license.update_one({
'email': email_raffleItem.email
}, {
'$set': {
'total_entries': user_email.get('total_entries') + 1,
'raffleItem': {
email_raffleItem.raffleItem:
{"entries": user_email.get('raffleItem').get(email_raffleItem.raffleItem).get('entries') + 1}
}
}
}, upsert=False)
return 'Successfully Entires'
elif user_email:
db.license.update_one({
'email': email_raffleItem.email
}, {
'$set': {
'total_entries': user_email.get('total_entries') + 1,
'raffleItem': {
email_raffleItem.raffleItem:
{"entries": 1}
}
}
}, upsert=False)
return 'Successfully Entires'
else:
return "Email not in DB"
as you can see I went the easy way where I do a check if a user email is in DB aswell as if the same user has a object "raffleItem". If the user doesn't have the "raffleItem" then we create on in elif
statement and vice versa in if statement (meaning that user has a object of raffleItem)
and basically what I am doing is that I create something called "entries" which adds up +1 everytime it code hits the line.
My question is in that case if is it possible to cut the code somehow with the if-elif-else statement because I have a feeling I am doing wrong and maybe not as good as it should be.
What I am trying to do is:
1. Check if user email is in db
2. Check if user has raffleItem object
if there is raffleItem object then we add +1 in entries
elif there is NO raffleItem object when we add entries and add 1 value.
else:
the user doesn't match anything of it.
Aucun commentaire:
Enregistrer un commentaire