python - insert and update list of dictionary in pymongo -
i have records in database date unique key. each record have date , result fields.
result field list of dictionaries, dictionary having 2 value. if new record comes same date, should appends dictionaries existing dictionary in distinct manner.
this code, print "updated records ", record
gives me updates distinct list of dictionary. updates command not reflect on database. database content remains same before.
def saveentity(self, record): try: self.collection.insert(record) print "mongo done" except exception: data = self.collection.find({'date': 2}) key in data: print "db record : ",key record['result'].extend([k k in key['result'] if k not in record['result']]) print "updated records ", record --- x key in data: self.collection.update( {'date' : 2}, {'result':record['result']}, true )
original content record['result']:
[{"a": 1, "city" : "p"}, {"b": 2, "city" : "a"}]
new content comes same date
[{"a": 1, "city" : "p"}, {"c": 3, "city" : "m"}]
updated content per code line x
[{'a': 1, 'city': 'p'}, {'city': 'm', 'c': 3}, {u'city': u'a', u'b': 2}]
please not u
here, dont know reason.
database content @ end
[{"a": 1, "city" : "p"}, {"b": 2, "city" : "a"}]
problem here is, should update database new appended list of dictionary, not
try including $set
update operator modifier in update:
self.collection.update({'date' : 2}, { '$set': {'result': record['result']} }, true)
Comments
Post a Comment