<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def upsert(self, null=False):
 """ Insert or Update Document :param null: whether update null values Wisely select unique field values as filter, Update with upsert=True """

        self._pre_save()
        self.validate()

        filter_ = self._upsert_filter()
        if filter_:
            update = self._upsert_update(filter_, null)

            if update['$set']:
                r = self._coll.find_one_and_update(filter_, update,
                                                   upsert=True, new=True)
                self._data['_id'] = r['_id']
        else:
            r = self._coll.insert_one(self._data)
            self._data['_id'] = r.inserted_id