如何使用对象保存或更新?

时间:2015-10-30 14:30:19

标签: python mongodb mongoengine upsert

我正在编写以下代码来upsert mongodb:

def save_qs(self, qs)
    try:
        existed = QuerySetRecord.objects.get(recordId=qs.recordId)
        if qs.count > existed.count:
            qs._id = existed._id
            qs.save()
    except DoesNotExist as e:
        qs.save()

我在_id方法中找到了mongoengine check save,如果obj有_id,请执行insert,如果没有则更新。

created = ('_id' not in doc or self._created or force_insert)

但我知道这不是那么好,我怎么能做得更好?

以下是QuerySet

class QuerySetRecord(Document):
    recordId = StringField(required=True)
    crawlerName = StringField(required=True)
    recordType = StringField(required=True)
    dataMap = DictField(required=True)
    priority = IntField()
    count = IntField()
    valid = BooleanField(required=True)

0 个答案:

没有答案