Skip to content

Commit 50c1245

Browse files
DOCSP-37002 - GridFS (#18)
Co-authored-by: Jordan Smith <[email protected]>
1 parent 331a720 commit 50c1245

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

source/fundamentals.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ Fundamentals
1212
:maxdepth: 1
1313

1414
/fundamentals/enterprise-authentication
15+
/fundamentals/gridfs
1516
/fundamentals/indexes
1617
/fundamentals/in-use-encryption
1718
/fundamentals/periodic-executors
1819
/fundamentals/type-hints
1920

2021
- :ref:`pymongo-enterprise-auth`
22+
- :ref:`pymongo-gridfs`
2123
- :ref:`pymongo-indexes`
2224
- :ref:`pymongo-in-use-encryption`
2325
- :ref:`pymongo-periodic-executors`

source/fundamentals/gridfs.txt

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,84 @@
1-
.. uses gridfs.rst
1+
.. _pymongo-gridfs:
22

3-
GridFS Example
4-
==============
3+
GridFS
4+
======
55

6-
.. code-block:: python
7-
8-
from pymongo import MongoClient
9-
10-
client = MongoClient()
11-
client.drop_database("gridfs_example")
6+
.. contents:: On this page
7+
:local:
8+
:backlinks: none
9+
:depth: 1
10+
:class: singlecol
1211

13-
This example shows how to use ``gridfs`` to store large binary
14-
objects (e.g. files) in MongoDB.
12+
.. facet::
13+
:name: genre
14+
:values: reference
15+
16+
.. meta::
17+
:keywords: binary large object, blob, file, storage
1518

16-
.. seealso:: The API docs for ``gridfs``.
17-
18-
.. seealso:: `This blog post
19-
<http://dirolf.com/2010/03/29/new-gridfs-implementation-for-pymongo.html>`_
20-
for some motivation behind this API.
19+
This guide shows how to use GridFS to store and retrieve large files
20+
in MongoDB.
2121

2222
Setup
2323
-----
2424

25-
We start by creating a ``~gridfs.GridFS`` instance to use:
25+
Each ``GridFS`` instance operates on a specific MongoDB database.
26+
To create a ``GridFS`` instance, call the ``GridFS()``
27+
constructor and pass in a MongoDB database:
2628

2729
.. code-block:: python
2830

29-
>>> from pymongo import MongoClient
30-
>>> import gridfs
31-
>>>
32-
>>> db = MongoClient().gridfs_example
33-
>>> fs = gridfs.GridFS(db)
34-
35-
Every ``~gridfs.GridFS`` instance is created with and will
36-
operate on a specific ``~pymongo.database.Database`` instance.
31+
>>> from pymongo import MongoClient
32+
>>> import gridfs
33+
>>>
34+
>>> db = MongoClient().gridfs_example
35+
>>> fs = gridfs.GridFS(db)
3736

3837
Saving and Retrieving Data
3938
--------------------------
4039

41-
The simplest way to work with ``gridfs`` is to use its key/value
42-
interface (the the ``~gridfs.GridFS.put`` method and
43-
the ``~gridfs.GridFS.get`` method methods). To write data to GridFS, use
44-
the ``~gridfs.GridFS.put`` method:
40+
GridFS offers a key-value-pair interface through the ``~gridfs.GridFS.put()`` and
41+
``~gridfs.GridFS.get()`` methods.
42+
43+
To write data to GridFS, use the ``~gridfs.GridFS.put()`` method:
4544

4645
.. code-block:: python
4746

48-
>>> a = fs.put(b"hello world")
47+
>>> a = fs.put("hello world")
4948

50-
the ``~gridfs.GridFS.put`` method creates a new file in GridFS, and returns
51-
the value of the file document's ``"_id"`` key. Given that ``"_id"``
52-
we can use the ``~gridfs.GridFS.get`` method to get back the contents of the
53-
file:
49+
The ``~gridfs.GridFS.put()`` method creates a new file in GridFS, and returns
50+
the value of the file's ``_id`` key. You can use the file's returned ``_id``
51+
value to retrieve the contents of the file:
5452

5553
.. code-block:: python
5654

57-
>>> fs.get(a).read()
58-
b'hello world'
55+
>>> fs.get(a).read()
56+
'hello world'
5957

60-
the ``~gridfs.GridFS.get`` method returns a file-like object, so we get the
61-
file's contents by calling the ``~gridfs.grid_file.GridOut.read`` method.
58+
The ``~gridfs.GridFS.get()`` method returns a file-like object. To read the
59+
file's contents, call the ``~gridfs.grid_file.GridOut.read()`` method. You can store
60+
any object that has a ``read()`` method in GridFS.
6261

63-
In addition to putting a ``str`` as a GridFS file, we can also
64-
put any file-like object (an object with a the ``read`` method
65-
method). GridFS will handle reading the file in chunk-sized segments
66-
automatically. We can also add additional attributes to the file as
67-
keyword arguments:
62+
The following code example shows how to add additional attributes to a file.
63+
To add an attribute, pass it as a named argument to the ``~gridfs.GridFS.put()`` method.
64+
To retrieve an attribute's value, call
65+
the ``~gridfs.GridFS.get()`` method to retrieve the file-like object,
66+
then use the attribute name to access each value on this object.
6867

6968
.. code-block:: python
7069

71-
>>> b = fs.put(fs.get(a), filename="foo", bar="baz")
72-
>>> out = fs.get(b)
73-
>>> out.read()
74-
b'hello world'
75-
>>> out.filename
76-
'foo'
77-
>>> out.bar
78-
'baz'
79-
>>> out.upload_date
80-
datetime.datetime(...)
81-
82-
The attributes we set in the ``~gridfs.GridFS.put`` method are stored in the
83-
file document, and retrievable after calling
84-
the ``~gridfs.GridFS.get`` method. Some attributes (like ``"filename"``) are
85-
special and are defined in the GridFS specification - see that
86-
document for more details.
70+
>>> b = fs.put(fs.get(a), filename="foo", bar="baz")
71+
>>> out = fs.get(b)
72+
>>> out.read()
73+
'hello world'
74+
>>> out.filename
75+
'foo'
76+
>>> out.bar
77+
'baz'
78+
>>> out.upload_date
79+
datetime.datetime(...)
80+
81+
.. note::
82+
83+
Some attributes (for example, ``"filename"``) are GridFS keywords and can't be
84+
assigned values.

0 commit comments

Comments
 (0)