Querying fields
The 'get' operations can retrieve fields from a record when you select it with its primary key value. To retrieve records based on secondary indexes, you need to query your records as showed in the Query fields page.
You have multiple operations at your disposition :
#
Creating the table for our example#
1 - Querying a single field value from a secondary indexYou can use query_field to retrieve a single field value from multiple records.
Specify the index you want to query (for example, profession). You can also specify your primary index (for example, userId), but DynamoDB enforces uniqueness on primary index values, which means that a query on the primary index will never return more than one record item, it is more efficient to use the 'get operations' detailed in Retrieving fields. The only exception would be if you wanted to add filter expression to a request on the primary index, as showcased in example 1.
Similarly to the get_field operation, you must specify the key_value
of the records you want
to query. And field_path
to select the field you want to retrieve in each record.
retrieved_records_value
will contain a dictionary where the keys will be the primary key values of each record
queried, and the values will be the matching field you requested for each record.
query_metadata
old information about the state of the query, that will be required if you need to paginate your
query result into multiple pages.
Note that when the combined size of the attributes you are retrieving from each of your queried records reach 1MB of
size, or when the number of scanned records reach the pagination_records_limit
parameter (if you specified it),
your query operation will automatically be split into multiple pages. If you know for a fact you will have only
a few records, you can use query_field
without worrying about pagination, but it would be better to always treat
results from query operations as potentially paginated, as showcased in the examples below.
#
2 - Querying a single field value from a secondary index and navigate trough pages managedThe easiest way to perform a paginated query for the retrieval of single field is to use the paginated_query_field.
Specify
You will be returned a Generator
, it is similar to an iterable, you can loop over it like a list, but does not has
a pre-determined length, and will create the items as you iterate over him (in our case, creating the items is sending a
database request to get the next page of query results).
Specify the primary key value of the records you want to retrieve the field from with the key_value
parameter.
Specify the field you want to retrieve with field_path
.
If no value has been found or if it did not pass the data validation, retrieved_value
will be None.
The typing with Optional[str]
is not required.
#
2 - Querying a single field value from a secondary index and navigate trough maximum amounts of pages managedSince the requests are being sent as you iterate over the records_paginator
, you can break out of the
loop at any time, which will stop sending requests for the next pages of query results.
For example, if you want to define a maximum amount of pages to iterate trough, you can keep track of the page index
(for example, with enumerate
), and break out of the records_paginator loop when you have reached your maximum
amount of pages.
Just make sure to break out of the loop at the end of it, after having used the query page you retrieved. Otherwise, if you were breaking at the start of the loop, for example as soon as you reach the sixth page, this sixth page would have been retrieved from the database for nothing. Instead, you could break at the end of the loop, of the fifth page.
#
3 - Querying a single field value from a secondary index and navigate trough pages manuallyInstead of using the paginated_query_field you can handle pagination of your requests manually with paginated_query_field.
Specify the index you want to query. You can etheir use a global secondary index defined in your table, or, by default if not specified, the primary index of your table. Since the primary key values in DynamoDB are unique, there is not a lot of uses for using query operations on the primary index opposed to the 'get operations' Retrieving fields, unless if you want to specify some filter condition, as showed in example 10.
Like a get_field operation, define the key_value where all the records with this , and the field_path of the field you want to retrieve.
Optionally, you can set pagination_records_limit
to define a maximum number of records to be scanned before the
current page of your query ends and a response is returned. If not specified, the page will end if all the records
matching the key_value for the index_name you specified as been scanned, or until the combined size of records
attributes you requested reach 1MB of size. Otherwise, the same rules applies, with the additional pagination_records_limit.
The navigation between paginated data will be done using the exclusive_start_key
parameter. Notice that the
query_field
returns both retrieved_records_value
and query_metadata
. The query_metadata contains 4
attributes. The count
attribute, representing how much records have been scanned in the current page of the query
(if you have set some filter conditions, the scanned count can be bigger than the number of records items being
returned). The has_reached_end
attribute, wether all the records matching the specified key_value and index_name
have been scanned, and you should end your query. Finally, the last_evaluated_key
attribute, a dictionary
containing informations about the last scanned record in your page, that can then be used as an
exclusive_start_key
to start a new page where you left off the last one, resulting in a progressive navigation
trough paginated query results.
If exclusive_start_key
is not defined or set to None, the page will start at the beginning of your records.
If no value has been found or if it did not pass the data validation, retrieved_value
will be None.
The typing with Optional[str]
is not required.
#
2 - Querying a single field value without data validation from a secondary indexThe retrieved data will be passed through the data validation of your table. If the value or some parts of it are invalid, they will be removed. The data validation is unforced client side by StructNoSQL, not on the database side which might cause the retrieved_value to be None or have less items than is actually present in the database.
If you need to disable the data_validation and actually retrieve any data present in the database without any checks or
alterations being done, you can disable it by passing False to the data_validation
parameter.
Notice that in our get_field
example, we typed the retrieved_value with Optional[str]
where we assumed that
if the value is not None (with Optional
), it will be a str
, as per the table model that would only return a
str
as a valid value. Now, since the data_validation is returned, our retrieved value could be anything, so we
type it as Optional[Any]
. Note that typing Python code is strictly optional and does not changes anything during
the execution of your code, it is only there to make your code clearer and for your IDE to give quality warnings.
#
3 - Querying multiple fields values with a multi-selectorIf you need to retrieve multiple fields that share the same parent path, you can use a multi-selector. Wrap the multiple fields names you want to retrieve inside parenthesis. This will be similar to using the get_multiple_fiends operation, where the multiple fields will be retrieved with a single database operation. You will be returned a dictionary with the keys will be all the fields names you requested, and their retrieved values if they were found.
No matter what, retrieved_values
will always be a dictionary containing as keys all the names of the
fields you tried to retrieve.
Even if the operation failed, the dictionary will be returned with a None
value for each
field.
You can safely access the fields values with brackets instead of using the .get
function on your dictionary.
#
4 - Querying multiple nested fields values with a multi-selectorYou can use multi-selectors even with nested fields, and even with query_kwargs. The only constraint, is the multi-selector must be the last 'path element' of your field path.
If you need to retrieve fields from totally different places, use get_multiple_fields showcased in the examples below.
#
5 - Querying multiple fields values with query_multiple_fieldsYou can use get_multiple_fields to retrieve multiple fields at once from the same
record with a single database operation.
Like update_field, you select the record you want to update with its primary key value passed in the key_value
parameter.
Specify the different fields you want to retrieved by passing a dictionary of FieldGetter.
Similarly to get_field, each FieldGetter requires the field_path parameter, and has an optional
query_kwargs parameter.
No matter what, retrieved_values
will always be a dictionary containing as keys all the names of the
fields you tried to retrieve.
Even if the operation failed, the dictionary will be returned with a None
value for each
field.
You can safely access the fields values with brackets instead of using the .get
function on your dictionary.
#
6 - Querying multiple fields values with query_multiple_fields without data validationThe retrieved data will be passed through the data validation of your table. If the value or some parts of it are invalid, they will be removed. The data validation is unforced client side by StructNoSQL, not on the database side which might cause the retrieved_value to be None or have less items than is actually present in the database.
If you need to disable the data_validation and actually retrieve any data present in the database without any checks or
alterations being done, you can disable it by passing False to the data_validation
parameter.