Generate a find command document.
(
coll: str,
spec: Mapping[str, Any],
projection: Optional[Union[Mapping[str, Any], Iterable[str]]],
skip: int,
limit: int,
batch_size: Optional[int],
options: Optional[int],
read_concern: ReadConcern,
collation: Optional[Mapping[str, Any]] = None,
session: Optional[_AgnosticClientSession] = None,
allow_disk_use: Optional[bool] = None,
)
| 214 | |
| 215 | |
| 216 | def _gen_find_command( |
| 217 | coll: str, |
| 218 | spec: Mapping[str, Any], |
| 219 | projection: Optional[Union[Mapping[str, Any], Iterable[str]]], |
| 220 | skip: int, |
| 221 | limit: int, |
| 222 | batch_size: Optional[int], |
| 223 | options: Optional[int], |
| 224 | read_concern: ReadConcern, |
| 225 | collation: Optional[Mapping[str, Any]] = None, |
| 226 | session: Optional[_AgnosticClientSession] = None, |
| 227 | allow_disk_use: Optional[bool] = None, |
| 228 | ) -> dict[str, Any]: |
| 229 | """Generate a find command document.""" |
| 230 | cmd: dict[str, Any] = {"find": coll} |
| 231 | if "$query" in spec: |
| 232 | cmd.update( |
| 233 | [ |
| 234 | (_MODIFIERS[key], val) if key in _MODIFIERS else (key, val) |
| 235 | for key, val in spec.items() |
| 236 | ] |
| 237 | ) |
| 238 | if "$explain" in cmd: |
| 239 | cmd.pop("$explain") |
| 240 | if "$readPreference" in cmd: |
| 241 | cmd.pop("$readPreference") |
| 242 | else: |
| 243 | cmd["filter"] = spec |
| 244 | |
| 245 | if projection: |
| 246 | cmd["projection"] = projection |
| 247 | if skip: |
| 248 | cmd["skip"] = skip |
| 249 | if limit: |
| 250 | cmd["limit"] = abs(limit) |
| 251 | if limit < 0: |
| 252 | cmd["singleBatch"] = True |
| 253 | if batch_size: |
| 254 | # When limit and batchSize are equal we increase batchSize by 1 to |
| 255 | # avoid an unnecessary killCursors. |
| 256 | if limit == batch_size: |
| 257 | batch_size += 1 |
| 258 | cmd["batchSize"] = batch_size |
| 259 | if read_concern.level and not (session and session.in_transaction): |
| 260 | cmd["readConcern"] = read_concern.document |
| 261 | if collation: |
| 262 | cmd["collation"] = collation |
| 263 | if allow_disk_use is not None: |
| 264 | cmd["allowDiskUse"] = allow_disk_use |
| 265 | if options: |
| 266 | cmd.update([(opt, True) for opt, val in _OPTIONS.items() if options & val]) |
| 267 | |
| 268 | return cmd |
| 269 | |
| 270 | |
| 271 | def _gen_get_more_command( |