Skip to content

Commit f7e8ddc

Browse files
authored
Merge pull request #17 from rustagir/DOCSP-37861-gridfs
DOCSP-37861: gridfs tutorial
2 parents 7490643 + 2313d98 commit f7e8ddc

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

source/tutorials.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ Tutorials
1616
/tutorials/change-stream/
1717
/tutorials/text-search/
1818
/tutorials/geo/
19+
/tutorials/gridfs/
1920

source/tutorials/gridfs.txt

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
.. _javars-gridfs:
2+
3+
======
4+
GridFS
5+
======
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
GridFS is a specification for storing and retrieving files that
14+
exceed the BSON document size limit of 16 MB. Instead of storing a large
15+
file in a single document, GridFS divides a file into parts, or chunks, and
16+
stores each of those chunks as separate documents.
17+
18+
When you query a GridFS store for a file, the driver reassembles the
19+
chunks as needed.
20+
21+
The code examples in this guide come from the `GridFSTour.java
22+
<{+driver-source-gh+}/blob/master/driver-reactive-streams/src/examples/reactivestreams/gridfs/GridFSTour.java>`__
23+
file in the driver source code GitHub repository.
24+
25+
Prerequisites
26+
-------------
27+
28+
You must include the following import statements in your program to run the
29+
code examples in this guide:
30+
31+
.. code-block:: java
32+
33+
import com.mongodb.reactivestreams.client.MongoClients;
34+
import com.mongodb.reactivestreams.client.MongoClient;
35+
import com.mongodb.reactivestreams.client.MongoDatabase;
36+
37+
import com.mongodb.client.gridfs.model.*;
38+
import com.mongodb.reactivestreams.client.gridfs.*;
39+
40+
import org.bson.Document;
41+
import org.bson.types.ObjectId;
42+
43+
import java.io.FileNotFoundException;
44+
import java.io.IOException;
45+
import java.nio.Buffer;
46+
import java.nio.ByteBuffer;
47+
import java.nio.charset.StandardCharsets;
48+
49+
import static com.mongodb.client.model.Filters.eq;
50+
import static reactivestreams.helpers.PublisherHelpers.toPublisher;
51+
52+
.. important::
53+
54+
This guide uses the ``Subscriber`` implementations, which are
55+
described in the :ref:`Quick Start Primer <javars-primer>`.
56+
57+
Connect to a MongoDB Deployment
58+
-------------------------------
59+
60+
First, connect to a MongoDB deployment and declare and define
61+
a ``MongoDatabase`` instance.
62+
63+
The following code connects to a standalone
64+
MongoDB deployment running on ``localhost`` on port ``27017``:
65+
66+
.. code-block:: java
67+
68+
MongoClient mongoClient = MongoClients.create();
69+
70+
To learn more about connecting to MongoDB deployments,
71+
see the :ref:`javars-connect` tutorial.
72+
73+
Create a GridFS Bucket
74+
----------------------
75+
76+
GridFS stores files in two collections:
77+
78+
- ``chunks``: stores the file chunks
79+
- ``files``: stores file metadata
80+
81+
The two collections are in a common bucket and the collection names
82+
are prefixed with the bucket name.
83+
84+
The driver provides the ``GridFSBuckets.create()`` method to
85+
create ``GridFSBucket`` instances:
86+
87+
.. code-block:: java
88+
89+
MongoDatabase myDatabase = mongoClient.getDatabase("mydb");
90+
91+
// Create a gridFSBucket using the default bucket name "fs"
92+
GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase);
93+
94+
You can pass a bucket name to the ``GridFSBuckets.create()`` method:
95+
96+
.. code-block:: java
97+
98+
// Create a gridFSBucket with a custom bucket name "files"
99+
GridFSBucket gridFSFilesBucket = GridFSBuckets.create(myDatabase, "files");
100+
101+
.. note::
102+
103+
GridFS automatically creates indexes on the ``files`` and ``chunks``
104+
collections when you upload data to the GridFS bucket.
105+
106+
Upload to GridFS
107+
----------------
108+
109+
The ``GridFSBucket.uploadFromPublisher()`` method reads the contents
110+
of ``Publisher<ByteBuffer>`` and saves it to the ``GridFSBucket`` instance.
111+
112+
You can use the ``GridFSUploadOptions`` type to configure the chunk size
113+
or include additional metadata.
114+
115+
The following example uploads the contents of a
116+
``Publisher<ByteBuffer>`` into ``GridFSBucket``:
117+
118+
.. code-block:: java
119+
120+
// Get the input publisher
121+
Publisher<ByteBuffer> publisherToUploadFrom = toPublisher(
122+
ByteBuffer
123+
.wrap("MongoDB Tutorial..".getBytes(StandardCharsets.UTF_8))
124+
);
125+
126+
// Create some custom options
127+
GridFSUploadOptions options = new GridFSUploadOptions()
128+
.chunkSizeBytes(1024)
129+
.metadata(new Document("type", "presentation"));
130+
131+
ObservableSubscriber<ObjectId> uploadSubscriber = new OperationSubscriber<>();
132+
gridFSBucket.uploadFromPublisher("mongodb-tutorial", publisherToUploadFrom, options).subscribe(uploadSubscriber);
133+
ObjectId fileId = uploadSubscriber.get().get(0);
134+
135+
Find Files Stored in GridFS
136+
---------------------------
137+
138+
To find the files stored in the ``GridFSBucket``, use the ``find()``
139+
method.
140+
141+
The following example prints out the filename of each file stored:
142+
143+
.. code-block:: java
144+
145+
ConsumerSubscriber<GridFSFile> filesSubscriber = new ConsumerSubscriber<>(gridFSFile ->
146+
System.out.println(" - " + gridFSFile.getFilename()));
147+
gridFSBucket.find().subscribe(filesSubscriber);
148+
filesSubscriber.await();
149+
150+
You can also provide a custom filter to limit the results returned.
151+
The following example prints out the filenames of all files in which the
152+
``contentType`` value is an ``image/png`` value in the user-defined metadata
153+
document:
154+
155+
.. code-block:: java
156+
157+
filesSubscriber = new ConsumerSubscriber<>(gridFSFile -> System.out.println("Found: " + gridFSFile.getFilename()));
158+
gridFSBucket.find(eq("metadata.contentType", "image/png")).subscribe(filesSubscriber);
159+
filesSubscriber.await();
160+
161+
Download from GridFS
162+
--------------------
163+
164+
The ``downloadToPublisher()`` method returns a ``Publisher<ByteBuffer>``
165+
that reads the contents from MongoDB.
166+
167+
To download a file by its file ``_id``, pass the ``_id`` to the method.
168+
The following example downloads a file by its file ``_id``:
169+
170+
.. code-block:: java
171+
172+
ObjectId fileId;
173+
ObservableSubscriber<ByteBuffer> downloadSubscriber = new OperationSubscriber<>();
174+
gridFSBucket.downloadToPublisher(fileId).subscribe(downloadSubscriber);
175+
176+
If you don't know the ``_id`` of the file but know the filename, then you
177+
can pass the filename to the ``downloadToPublisher()`` method. By
178+
default, it will download the latest version of the file. Use the
179+
``GridFSDownloadOptions`` class to configure which version to download.
180+
181+
The following example downloads the original version of the file named
182+
``mongodb-tutorial``:
183+
184+
.. code-block:: java
185+
186+
GridFSDownloadOptions downloadOptions = new GridFSDownloadOptions().revision(0);
187+
downloadSubscriber = new OperationSubscriber<>();
188+
gridFSBucket.downloadToPublisher("mongodb-tutorial", downloadOptions).subscribe(downloadSubscriber);
189+
190+
Rename Files
191+
------------
192+
193+
If you need to rename a file, then use the ``rename()`` method.
194+
195+
The following example renames a file to ``mongodbTutorial``:
196+
197+
.. code-block:: java
198+
199+
ObjectId fileId; //ObjectId of a file uploaded to GridFS
200+
gridFSBucket.rename(fileId, "mongodbTutorial").subscribe(new ObservableSubscriber<Void>());
201+
202+
.. note::
203+
204+
The ``rename()`` method requires an ``ObjectId`` rather than a ``filename`` to
205+
ensure the correct file is renamed.
206+
207+
To rename multiple revisions of the same filename, first retrieve the
208+
full list of files. Then, for every file that should be renamed,
209+
run ``rename()`` with the corresponding ``_id``.
210+
211+
Delete Files
212+
------------
213+
214+
To delete a file from the ``GridFSBucket``, use the ``delete()`` method.
215+
216+
The following example deletes a file from the ``GridFSBucket``:
217+
218+
.. code-block:: java
219+
220+
ObjectId fileId; //ObjectId of a file uploaded to GridFS
221+
gridFSBucket.delete(fileId).subscribe(new ObservableSubscriber<Void>());

0 commit comments

Comments
 (0)