Executes a N1QL query against the scope. .. note:: The query is executed lazily in that it is executed once iteration over the :class:`~couchbase.result.QueryResult` begins. .. note:: Scope-level queries are only supported on Couchbase Server ver
(self,
statement, # type: str
*options, # type: QueryOptions
**kwargs # type: Dict[str, Any]
)
| 68 | return Collection(self, name) |
| 69 | |
| 70 | def query(self, |
| 71 | statement, # type: str |
| 72 | *options, # type: QueryOptions |
| 73 | **kwargs # type: Dict[str, Any] |
| 74 | ) -> QueryResult: |
| 75 | """Executes a N1QL query against the scope. |
| 76 | |
| 77 | .. note:: |
| 78 | The query is executed lazily in that it is executed once iteration over the |
| 79 | :class:`~couchbase.result.QueryResult` begins. |
| 80 | |
| 81 | .. note:: |
| 82 | Scope-level queries are only supported on Couchbase Server versions that support scopes and collections. |
| 83 | |
| 84 | .. seealso:: |
| 85 | * :class:`~couchbase.management.queries.QueryIndexManager`: For how to manage query indexes. |
| 86 | * :meth:`~couchbase.cluster.Cluster.query`: For how to execute cluster-level queries. |
| 87 | |
| 88 | Args: |
| 89 | statement (str): The N1QL statement to execute. |
| 90 | options (:class:`~couchbase.options.QueryOptions`): Optional parameters for the query operation. |
| 91 | **kwargs (Dict[str, Any]): keyword arguments that can be used in place or to |
| 92 | override provided :class:`~couchbase.options.QueryOptions` |
| 93 | |
| 94 | Returns: |
| 95 | :class:`~couchbase.result.QueryResult`: An instance of a :class:`~couchbase.result.QueryResult` which |
| 96 | provides access to iterate over the query results and access metadata and metrics about the query. |
| 97 | |
| 98 | Examples: |
| 99 | Simple query:: |
| 100 | |
| 101 | q_res = scope.query('SELECT * FROM `inventory` WHERE country LIKE 'United%' LIMIT 2;') |
| 102 | for row in q_res.rows(): |
| 103 | print(f'Found row: {row}') |
| 104 | |
| 105 | Simple query with positional parameters:: |
| 106 | |
| 107 | from couchbase.options import QueryOptions |
| 108 | |
| 109 | # ... other code ... |
| 110 | |
| 111 | q_str = 'SELECT * FROM `inventory` WHERE country LIKE $1 LIMIT $2;' |
| 112 | q_res = scope.query(q_str, QueryOptions(positional_parameters=['United%', 5])) |
| 113 | for row in q_res.rows(): |
| 114 | print(f'Found row: {row}') |
| 115 | |
| 116 | Simple query with named parameters:: |
| 117 | |
| 118 | from couchbase.options import QueryOptions |
| 119 | |
| 120 | # ... other code ... |
| 121 | |
| 122 | q_str = 'SELECT * FROM `travel-sample` WHERE country LIKE $country LIMIT $lim;' |
| 123 | q_res = scope.query(q_str, QueryOptions(named_parameters={'country': 'United%', 'lim':2})) |
| 124 | for row in q_res.rows(): |
| 125 | print(f'Found row: {row}') |
| 126 | |
| 127 | Retrieve metadata and/or metrics from query:: |
no test coverage detected