|
23 | 23 | from gridfs.errors import (NoFile, |
24 | 24 | UnsupportedAPI) |
25 | 25 | from gridfs.grid_file import (GridIn, |
26 | | - GridOut) |
| 26 | + GridOut, |
| 27 | + GridOutCursor) |
27 | 28 | from pymongo import (MongoClient, |
28 | 29 | ASCENDING, |
29 | 30 | DESCENDING) |
@@ -278,6 +279,72 @@ def list(self): |
278 | 279 | name for name in self.__files.distinct("filename") |
279 | 280 | if name is not None] |
280 | 281 |
|
| 282 | + def find(self, *args, **kwargs): |
| 283 | + """Query GridFS for files. |
| 284 | +
|
| 285 | + Returns a cursor that iterates across files matching |
| 286 | + arbitrary queries on the files collection. Can be combined |
| 287 | + with other modifiers for additional control. For example |
| 288 | +
|
| 289 | + >>> for grid_out in fs.find({"filename": "lisa.txt"}, timeout=False): |
| 290 | + >>> data = grid_out.read() |
| 291 | +
|
| 292 | + would iterate through all versions of "lisa.txt" stored in GridFS. |
| 293 | + Note that setting timeout to False may be important to prevent the |
| 294 | + cursor from timing out during long multi-file processing work. |
| 295 | +
|
| 296 | + As another example, the call |
| 297 | +
|
| 298 | + >>> most_recent_three = fs.find().sort("uploadDate", -1).limit(3) |
| 299 | +
|
| 300 | + would return a cursor to the three most recently uploaded files |
| 301 | + in GridFS. |
| 302 | +
|
| 303 | + Follows a similar interface to |
| 304 | + :meth:`~pymongo.collection.Collection.find` |
| 305 | + in :class:`~pymongo.collection.Collection`. |
| 306 | +
|
| 307 | + :Parameters: |
| 308 | + - `spec` (optional): a SON object specifying elements which |
| 309 | + must be present for a document to be included in the |
| 310 | + result set |
| 311 | + - `skip` (optional): the number of files to omit (from |
| 312 | + the start of the result set) when returning the results |
| 313 | + - `limit` (optional): the maximum number of results to |
| 314 | + return |
| 315 | + - `timeout` (optional): if True (the default), any returned |
| 316 | + cursor is closed by the server after 10 minutes of |
| 317 | + inactivity. If set to False, the returned cursor will never |
| 318 | + time out on the server. Care should be taken to ensure that |
| 319 | + cursors with timeout turned off are properly closed. |
| 320 | + - `sort` (optional): a list of (key, direction) pairs |
| 321 | + specifying the sort order for this query. See |
| 322 | + :meth:`~pymongo.cursor.Cursor.sort` for details. |
| 323 | + - `max_scan` (optional): limit the number of file documents |
| 324 | + examined when performing the query |
| 325 | + - `read_preference` (optional): The read preference for |
| 326 | + this query. |
| 327 | + - `tag_sets` (optional): The tag sets for this query. |
| 328 | + - `secondary_acceptable_latency_ms` (optional): Any replica-set |
| 329 | + member whose ping time is within secondary_acceptable_latency_ms of |
| 330 | + the nearest member may accept reads. Default 15 milliseconds. |
| 331 | + **Ignored by mongos** and must be configured on the command line. |
| 332 | + See the localThreshold_ option for more information. |
| 333 | + - `compile_re` (optional): if ``False``, don't attempt to compile |
| 334 | + BSON regex objects into Python regexes. Return instances of |
| 335 | + :class:`~bson.regex.Regex` instead. |
| 336 | +
|
| 337 | + Raises :class:`TypeError` if any of the arguments are of |
| 338 | + improper type. Returns an instance of |
| 339 | + :class:`~gridfs.grid_file.GridOutCursor` |
| 340 | + corresponding to this query. |
| 341 | +
|
| 342 | + .. versionadded:: 2.7 |
| 343 | + .. mongodoc:: find |
| 344 | + .. _localThreshold: http://docs.mongodb.org/manual/reference/mongos/#cmdoption-mongos--localThreshold |
| 345 | + """ |
| 346 | + return GridOutCursor(self.__collection, *args, **kwargs) |
| 347 | + |
281 | 348 | def exists(self, document_or_id=None, **kwargs): |
282 | 349 | """Check if a file exists in this instance of :class:`GridFS`. |
283 | 350 |
|
|
0 commit comments