Queryset¶
-
class
yadm.queryset.
BaseQuerySet
(db, document_class, *, cache=None, criteria=None, projection=None, sort=None, slice=None, batch_size=None, collection_params=None)¶ Query builder.
Parameters: - db –
- document_class –
- cache –
- criteria (dict) –
- projection (dict) –
- sort (list) –
- slice (slice) –
- collection_params (dict) –
-
batch_size
(batch_size)¶ Setup batch size to cursor for this queryset.
-
cache
¶ Queryset cache object.
-
copy
(*, cache=None, criteria=None, projection=None, sort=None, slice=None, batch_size=None, collection_params=None)¶ Copy queryset with new parameters.
Only keywords arguments is alowed. Parameters simply replaced with given arguments.
Parameters: - cache –
- criteria (dict) –
- projection (dict) –
- sort (list) –
- slice (slice) –
- collection_params (dict) –
Returns: new
yadm.queryset.QuerySet
object
-
fields
(*fields)¶ Get only setted fields.
Update projection with fields.
Parameters: fields (str) – Returns: new yadm.queryset.QuerySet
qs('field', 'field2')
-
fields_all
()¶ Clear projection.
-
find
(criteria=None, projection=None)¶ Return queryset copy with new criteria and projection.
Parameters: - criteria (dict) – update queryset’s criteria
- projection (dict) – set queryset’s projection
Returns: qs({'field': {'$gt': 3}}, {'field': True})
-
read_preference
(read_preference)¶ Setup readPreference.
Return new QuerySet instance.
Deprecated since 1.4.0. Use collection_params argument in copy.
-
read_primary
(preferred=False)¶ Return queryset with setupd read concern for primary.
If preferred argument is True, PrimaryPreferred is used else Primary.
-
sort
(*sort)¶ Return queryset with sorting.
Parameters: sort (tuples) – tuples with two items: (‘field_name’, sort_order_as_int). qs.sort(('field_1', 1), ('field_2', -1))
-
class
yadm.queryset.
NotFoundBehavior
¶ An enumeration.
-
exception
yadm.queryset.
NotFoundError
¶
-
class
yadm.queryset.
QuerySet
(db, document_class, *, cache=None, criteria=None, projection=None, sort=None, slice=None, batch_size=None, collection_params=None)¶ -
bulk
()¶ Return map {id: object}.
Returns: dict
-
count
()¶ Count documents in queryset.
Returns: int
-
distinct
(field)¶ Distinct query.
Parameters: field (str) – field for distinct Returns: list with result data
-
find_and_modify
(update=None, *, upsert=False, full_response=False, new=False, **kwargs)¶ Execute $findAndModify query.
Parameters: - update (dict) – see second argument to update()
- upsert (bool) – insert if object doesn’t exist (default False)
- full_response (bool) – return the entire response object from the server (default False)
- new – return updated rather than original object (default False)
- kwargs – any other options the findAndModify command supports can be passed here
Returns: yadm.documents.Document
or None
-
find_in
(comparable, field='_id', *, not_found=<NotFoundBehavior.SKIP: 'skip'>)¶ Build ordered $in-query.
Creates a query of the form {field: {‘$in’: comparable}} and returns the generator of documents with the same order as an elements in the argument ‘comparable’.
Parameters: - comparable (list) – values for compare in a query
- field (str) – field name of the document for comparison
- not_found – flag determines the behavior if the document with the specified value is not found
Returns: generator of docs
- not_found argument can take the following values:
- ‘none’: If a document can not be found then a generator
- will return None.
- ‘skip’: if a document can not be found then a generator
- will pass element.
- ‘error’: if a document can not be found then a generator
- raise
yadm.queryset.DocNotFoundError
exception.
-
find_one
(criteria=None, projection=None, *, exc=None)¶ Find and return only one document.
Parameters: - criteria (dict) – update queryset’s criteria
- projection (dict) – update queryset’s projection
- exc (Exception) – raise given exception if not found
Returns: yadm.documents.Document
or Noneqs({'field': {'$gt': 3}}, {'field': True})
-
ids
()¶ Return all objects ids from queryset.
-
join
(*field_names)¶ Create yadm.Join object, join field_names and return it.
Parameters: fields_names (str) – fields for join Returns: new yadm.join.Join
- Next algorithm for join:
- Get all documents from queryset;
- Aggegate all ids from requested fields;
- Make $in queries for get joined documents;
- Bind joined documents to objects from first queryset;
Join object is instance of abc.Sequence.
-
remove
(*, multi=True)¶ Remove documents in queryset.
Parameters: multi (bool) – if False, remove only first finded document (default True)
-
update
(update, *, multi=True, upsert=False)¶ Update documents in queryset.
Parameters: - update (dict) – update query
- multi (bool) – update all matched documents (default True)
- upsert (bool) – insert if not found (default False)
Returns: update result
-
with_id
(_id)¶ Find document with id.
This method is deprecated. Use find_one.
Parameters: _id – id of searching document Returns: yadm.documents.Document
or None
-