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

Method get

mongoengine/queryset/base.py:262–288  ·  view source on GitHub ↗

Retrieve the matching object raising :class:`~mongoengine.queryset.MultipleObjectsReturned` or `DocumentName.MultipleObjectsReturned` exception if multiple results and :class:`~mongoengine.queryset.DoesNotExist` or `DocumentName.DoesNotExist` if no results are found.

(self, *q_objs, **query)

Source from the content-addressed store, hash-verified

260 return queryset
261
262 def get(self, *q_objs, **query):
263 """Retrieve the matching object raising
264 :class:`~mongoengine.queryset.MultipleObjectsReturned` or
265 `DocumentName.MultipleObjectsReturned` exception if multiple results
266 and :class:`~mongoengine.queryset.DoesNotExist` or
267 `DocumentName.DoesNotExist` if no results are found.
268 """
269 queryset = self.clone()
270 queryset = queryset.order_by().limit(2)
271 queryset = queryset.filter(*q_objs, **query)
272
273 try:
274 result = next(queryset)
275 except StopIteration:
276 msg = "%s matching query does not exist." % queryset._document._class_name
277 raise queryset._document.DoesNotExist(msg)
278
279 try:
280 # Check if there is another match
281 next(queryset)
282 except StopIteration:
283 return result
284
285 # If we were able to retrieve the 2nd doc, raise the MultipleObjectsReturned exception.
286 raise queryset._document.MultipleObjectsReturned(
287 "2 or more items returned, instead of 1"
288 )
289
290 def create(self, **kwargs):
291 """Create new object. Returns the saved object instance."""

Callers 12

upsert_oneMethod · 0.95
__init__Method · 0.45
insertMethod · 0.45
deleteMethod · 0.45
no_sub_classesMethod · 0.45
distinctMethod · 0.45
map_reduceMethod · 0.45
exec_jsMethod · 0.45
queryFunction · 0.45
updateFunction · 0.45
__get__Method · 0.45

Calls 4

cloneMethod · 0.95
limitMethod · 0.80
order_byMethod · 0.80
filterMethod · 0.45

Tested by

no test coverage detected