Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions gridfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,36 @@ def list(self):
name for name in self.__files.distinct("filename")
if name is not None]

def find_one(self, spec_or_id=None, *args, **kwargs):
"""Get a single file from gridfs.

All arguments to :meth:`find` are also valid arguments for
:meth:`find_one`, although any `limit` argument will be
ignored. Returns a single gridfs file, or ``None`` if no matching
file is found. For example::

file = fs.find_one({"filename": "lisa.txt"})

:Parameters:

- `spec_or_id` (optional): a dictionary specifying
the query to be performing OR any other type to be used as
the value for a query for ``"_id"`` in the file collection.

- `*args` (optional): any additional positional arguments are
the same as the arguments to :meth:`find`.

- `**kwargs` (optional): any additional keyword arguments
are the same as the arguments to :meth:`find`.
"""
if spec_or_id is not None and not isinstance(spec_or_id, dict):
spec_or_id = {"_id": spec_or_id}

for file in self.find(spec_or_id, *args, **kwargs):
return file

return None

def find(self, *args, **kwargs):
"""Query GridFS for files.

Expand Down