MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / modify

Method modify

mongoengine/document.py:310–353  ·  view source on GitHub ↗

Perform an atomic update of the document in the database and reload the document object using updated version. Returns True if the document has been updated or False if the document in the database doesn't match the query. .. note:: All unsaved changes that have bee

(self, query=None, **update)

Source from the content-addressed store, hash-verified

308 return data
309
310 def modify(self, query=None, **update):
311 """Perform an atomic update of the document in the database and reload
312 the document object using updated version.
313
314 Returns True if the document has been updated or False if the document
315 in the database doesn't match the query.
316
317 .. note:: All unsaved changes that have been made to the document are
318 rejected if the method returns True.
319
320 :param query: the update will be performed only if the document in the
321 database matches the query
322 :param update: Django-style update keyword arguments
323 """
324 if query is None:
325 query = {}
326
327 if self.pk is None:
328 raise InvalidDocumentError("The document does not have a primary key.")
329
330 id_field = self._meta["id_field"]
331 query = query.copy() if isinstance(query, dict) else query.to_query(self)
332
333 if id_field not in query:
334 query[id_field] = self.pk
335 elif query[id_field] != self.pk:
336 raise InvalidQueryError(
337 "Invalid document modify query: it must modify only this document."
338 )
339
340 # Need to add shard key to query, or you get an error
341 query.update(self._object_key)
342
343 updated = self._qs(**query).modify(new=True, **update)
344 if updated is None:
345 return False
346
347 for field in self._fields_ordered:
348 setattr(self, field, self._reload(field, updated[field]))
349
350 self._changed_fields = updated._changed_fields
351 self._created = False
352
353 return True
354
355 def save(
356 self,

Calls 6

_qsMethod · 0.95
_reloadMethod · 0.95
InvalidQueryErrorClass · 0.90
to_queryMethod · 0.80
updateMethod · 0.45