2
I’m doing a program that will perform numerous insertions in Mongo (about 500,000). But I noticed doing some tests, that the more entries in Mongo I have, the longer is the insertion.
My code:
def savePostRecommendation(uid, recs, db):
temp = db.posts.find_one({'uid':uid})
if(temp is None):
db.posts.insert({'uid':uid,'recs':recs})
else:
db.posts.update({'uid':uid},{'uid':uid,'recs':recs})
My theory:
I’m making an inquiry to see if there’s an entry temp = db.posts.find_one({'uid':uid})
, because if the function was only db.posts.Insert({'uid':uid,'Recs':Recs}), if the code was executed again there would be two entries in the Mongo with the same values. So I think when more entries in the Mongo I own this search that I do takes longer and longer.
My question is: How do I insert a value defining my attribute uid
as a primary key like SQL does. So then I insert the input without checking if it exists and that there is no uids
duplicates?