python - MongoDB generating same ID between inserts -
i using pymongo , trying insert dicts mongodb database. dictionaries this
{ "name" : "abc", "jobs" : [ { "position" : "systems engineer (data analyst)", "time" : [ "october 2014", "may 2015" ], "is_current" : 1, "location" : "xyz", "organization" : "xyz" }, { "position" : "systems engineer (mdm support lead)", "time" : [ "january 2014", "october 2014" ], "is_current" : 1, "location" : "xxx", "organization" : "xxx" }, { "position" : "asst. systems engineer (etl support executive)", "time" : [ "may 2012", "december 2013" ], "is_current" : 1, "location" : "zzz", "organization" : "xzx" }, ], "location" : "buffalo, new york", "education" : [ { "school" : "state university of new york @ buffalo - school of management", "major" : "management information systems, general", "degree" : "master of science (ms), " }, { "school" : "rajiv gandhi prodyogiki vishwavidyalaya", "major" : "electrical , electronics engineering", "degree" : "bachelor of engineering (b.e.), " } ], "id" : "abc123", "profile_link" : "example.com", "html_source" : "<html> some_source_code </html>" } i getting error:
pymongo.errors.duplicatekeyerror: e11000 duplicate key error index: linkedin_db.employee_info.$id dup key: { : objectid('56b64f6071c54604f02510a8') }
when run program 1st document gets inserted when insert second document error. when start script again document not inserted because of error inserted , error comes next document , continues.
clearly mognodb using same objecid during 2 inserts. don't understand why mongodb failing generate unique id new documents.
my code save passed data:
class mongosave: """ pass collection_name , dict data module stores passed dict in collection """ def __init__(self): self.connection = pymongo.mongoclient() self.db = self.connection['linkedin_db'] def _exists(self, id): #to check if user alredy exists return true if list(self.collection.find({'id': id})) else false def save(self, collection_name, data): self.collection = self.db[collection_name] if not self._exists(data['id']): print (data['id']) self.collection.insert(data) else: self.collection.update({'id':data['id']}, {"$set": data}) i can figure out why happening. appreciated.
the problem save method using field called "id" decide if should insert or upsert. want use "_id" instead. you can read _id field , index here. pymongo automatically adds _id document if 1 not present. you can read more here.
Comments
Post a Comment