Skip to content

Commit 6c02192

Browse files
authored
DOCSP-35707: merge gridfs into mongo (#339)
* DOCSP-35707: merge gridfs into mongo * add links * add whats new item * remove error from gridfsbucket method * fix build error
1 parent 0e558da commit 6c02192

File tree

8 files changed

+119
-110
lines changed

8 files changed

+119
-110
lines changed

source/fundamentals/crud/read-operations/limit.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The following read operations take an options object as a parameter:
6161

6262
- ``Find()``
6363
- ``CountDocuments()``
64-
- ``gridfs.Bucket.Find()``
64+
- ``GridFSBucket.Find()``
6565

6666
If the limit is ``0`` or exceeds the number of matched
6767
documents, the method returns all the documents. If the limit is a
@@ -223,4 +223,4 @@ guide, see the following API Documentation:
223223
- `FindOptions.SetSkip() <{+api+}/mongo/options#FindOptions.SetSkip>`__
224224
- `Aggregate() <{+api+}/mongo#Collection.Aggregate>`__
225225
- `CountDocuments() <{+api+}/mongo#Collection.CountDocuments>`__
226-
- `gridfs.Bucket.Find() <{+api+}/mongo/gridfs#Bucket.Find>`__
226+
- `GridFSBucket.Find() <{+api+}/mongo#GridFSBucket.Find>`__

source/fundamentals/crud/read-operations/skip.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The following read operations take an options object as a parameter:
5858
- ``Find()``
5959
- ``FindOne()``
6060
- ``CountDocuments()``
61-
- ``gridfs.Bucket.Find()``
61+
- ``GridFSBucket.Find()``
6262

6363
If the number of documents exceeds the number of matched documents for a
6464
query, that query returns no documents.
@@ -177,4 +177,4 @@ guide, see the following API Documentation:
177177
- `FindOptions.SetSkip() <{+api+}/mongo/options#FindOptions.SetSkip>`__
178178
- `Aggregate() <{+api+}/mongo#Collection.Aggregate>`__
179179
- `CountDocuments() <{+api+}/mongo#Collection.CountDocuments>`__
180-
- `gridfs.Bucket.Find() <{+api+}/mongo/gridfs#Bucket.Find>`__
180+
- `GridFSBucket.Find() <{+api+}/mongo#GridFSBucket.Find>`__

source/fundamentals/crud/read-operations/sort.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The following operations take options as a parameter:
5858
- ``FindOneAndDelete()``
5959
- ``FindOneAndUpdate()``
6060
- ``FindOneAndReplace()``
61-
- ``gridfs.Bucket.Find()``
61+
- ``GridFSBucket.Find()``
6262

6363
You can set an **ascending** or **descending** sort direction.
6464

@@ -283,4 +283,4 @@ guide, see the following API Documentation:
283283
- `FindOneAndDelete() <{+api+}/mongo#Collection.FindOneAndDelete>`__
284284
- `FindOneAndUpdate() <{+api+}/mongo#Collection.FindOneAndUpdate>`__
285285
- `FindOneAndReplace() <{+api+}/mongo#Collection.FindOneAndReplace>`__
286-
- `gridfs.Bucket.Find() <{+api+}/mongo/gridfs#Bucket.Find>`__
286+
- `GridFSBucket.Find() <{+api+}/mongo#GridFSBucket.Find>`__

source/fundamentals/gridfs.txt

Lines changed: 93 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ and organization of the file storage.
2929

3030
Use GridFS if the size of your files exceeds the BSON document size limit of
3131
16 MB. GridFS also helps you access files without loading the entire file
32-
into memory. For more detailed information on whether GridFS is suitable for
33-
your use case, see the :manual:`GridFS server manual page </core/gridfs>`.
32+
into memory. To learn more about whether GridFS is suitable for
33+
your use case, see :manual:`GridFS </core/gridfs>` in the Server manual.
3434

3535
How GridFS Works
3636
----------------
@@ -39,8 +39,8 @@ GridFS organizes files in a **bucket**, a group of MongoDB collections
3939
that contain the chunks of files and information describing them. The
4040
bucket contains the following collections:
4141

42-
- The ``chunks`` collection, which stores the binary file chunks.
43-
- The ``files`` collection, which stores the file metadata.
42+
- ``chunks``, which stores the binary file chunks
43+
- ``files``, which stores the file metadata
4444

4545
When you create a new GridFS bucket, the driver creates the preceding
4646
collections. The default bucket name ``fs`` prefixes the collection names,
@@ -49,8 +49,9 @@ bucket during the first write operation.
4949

5050
The driver also creates an index on each collection to ensure efficient
5151
retrieval of the files and related metadata. The driver creates indexes
52-
if they do not already exist and when the bucket is empty. For more information
53-
on GridFS indexes, see the server manual page on :manual:`GridFS Indexes </core/gridfs/#gridfs-indexes>`.
52+
if they do not already exist and when the bucket is empty. To learn more about
53+
GridFS indexes, see :manual:`GridFS Indexes
54+
</core/gridfs/#gridfs-indexes>` in the Server manual.
5455

5556
When storing files with GridFS, the driver splits the files into smaller
5657
chunks, each represented by a separate document in the ``chunks`` collection.
@@ -62,15 +63,14 @@ how GridFS splits the uploaded files:
6263
:alt: A diagram that shows how GridFS uploads a file to a bucket
6364

6465
When retrieving files, GridFS fetches the metadata from the ``files``
65-
collection in the specified bucket, then uses that information to reconstruct
66+
collection in the specified bucket then uses that information to reconstruct
6667
the file from documents in the ``chunks`` collection. You can read the file
6768
into memory or output it to a stream.
6869

69-
Use GridFS
70-
----------
70+
GridFS Operations
71+
-----------------
7172

72-
To learn about GridFS operations and how to perform them, navigate to the
73-
following sections:
73+
The following sections describe how to perform GridFS operations:
7474

7575
- :ref:`<golang-create-bucket>`
7676
- :ref:`<golang-upload-files>`
@@ -87,73 +87,74 @@ Create a GridFS Bucket
8787

8888
To store or retrieve files from GridFS, create a bucket or get a reference to
8989
an existing bucket on a MongoDB database. To create a ``GridFSBucket`` instance,
90-
call the ``NewBucket()`` method with a database parameter:
90+
call the ``GridFSBucket()`` method on a ``Database`` instance, as shown
91+
in the following code:
9192

9293
.. code-block:: go
9394

94-
db := client.Database("db")
95-
bucket, err := gridfs.NewBucket(db)
96-
if err != nil {
97-
panic(err)
98-
}
95+
db := client.Database("myDB")
96+
bucket := db.GridFSBucket()
9997

10098
.. note::
10199

102-
If a GridFS bucket already exists, the ``NewBucket()`` method returns a
100+
If a GridFS bucket already exists, the ``GridFSBucket()`` method returns a
103101
reference to the bucket rather than instantiating a new one.
104102

105-
By default, the new bucket is named ``fs``. To instantiate a bucket with a
106-
custom name, call the ``SetName()`` method on a ``BucketOptions`` instance as
107-
follows:
103+
By default, the driver sets the name of the bucket to ``fs``. To
104+
create a bucket with a custom name, call the ``SetName()`` method
105+
on a ``BucketOptions`` instance, as shown in the following code:
108106

109107
.. code-block:: go
110108

111-
db := client.Database("db")
112-
opts := options.GridFSBucket().SetName("custom name")
113-
bucket, err := gridfs.NewBucket(db, opts)
114-
115-
if err != nil {
116-
panic(err)
117-
}
109+
db := client.Database("myDB")
110+
bucketOpts := options.GridFSBucket().SetName("myCustomBucket")
111+
112+
bucket := db.GridFSBucket(bucketOpts)
118113

119114
.. _golang-upload-files:
120115

121116
Upload Files
122117
~~~~~~~~~~~~
123118

124-
You can upload a file into a GridFS bucket in one of the following ways:
119+
You can upload a file into a GridFS bucket by using one of the following
120+
methods:
125121

126-
- Use the ``UploadFromStream()`` method, which reads from an input stream.
127-
- Use the ``OpenUploadStream()`` method, which writes to an output stream.
122+
- ``UploadFromStream()``, which reads from an input stream
123+
- ``OpenUploadStream()``, which writes to an output stream
128124

129-
For either upload process, you can specify configuration information on an instance
130-
of ``UploadOptions``. For a full list of ``UploadOptions`` fields, visit the
131-
`API documentation <{+api+}/mongo/options#UploadOptions>`__.
125+
For either upload process, you can specify configuration information by creating
126+
an ``UploadOptions`` instance. To view a full list of options, see the
127+
`UploadOptions API documentation <{+api+}/mongo/options#UploadOptions>`__.
132128

133129
Upload with an Input Stream
134130
```````````````````````````
135131

136132
To upload a file with an input stream, use the ``UploadFromStream()`` method
137-
with the following parameters:
133+
and include the following parameters:
138134

139-
- Your file name
140-
- An ``io.Reader``, with your opened file as a parameter
141-
- An optional ``opts`` parameter to modify the behavior of ``UploadFromStream()``
135+
- File name
136+
- ``io.Reader`` instance, including your opened file as a parameter
137+
- ``opts`` parameter to modify the behavior of ``UploadFromStream()``
142138

143-
The following code example reads from a file called ``file.txt`` and uploads the
144-
content to a GridFS bucket. It uses an ``opts`` parameter to set file metadata:
139+
The following code example reads from a file called ``file.txt``,
140+
creates an ``opts`` parameter to set file metadata, and uploads the
141+
content to a GridFS bucket:
145142

146143
.. io-code-block::
147144
:copyable: true
148145

149146
.. input::
150147
:language: go
151148

152-
file, err := os.Open("path/to/file.txt")
149+
file, err := os.Open("home/documents/file.txt")
153150
uploadOpts := options.GridFSUpload().SetMetadata(bson.D{{"metadata tag", "first"}})
154151

155-
objectID, err := bucket.UploadFromStream("file.txt", io.Reader(file),
156-
uploadOpts)
152+
objectID, err := bucket
153+
.UploadFromStream(
154+
"file.txt",
155+
io.Reader(file),
156+
uploadOpts
157+
)
157158
if err != nil {
158159
panic(err)
159160
}
@@ -164,20 +165,19 @@ content to a GridFS bucket. It uses an ``opts`` parameter to set file metadata:
164165
:language: none
165166
:visible: false
166167

167-
New file uploaded with ID 62e00...
168-
168+
New file uploaded with ID ...
169169

170170
Upload with an Output Stream
171171
````````````````````````````
172172

173173
To upload a file with an output stream, use the ``OpenUploadStream()`` method
174-
with the following parameters:
174+
and include the following parameters:
175175

176-
- Your file name
177-
- An optional ``opts`` parameter to modify the behavior of ``OpenUploadStream()``
176+
- File name
177+
- ``opts`` parameter to modify the behavior of ``OpenUploadStream()``
178178

179179
The following code example opens an upload stream on a GridFS bucket and sets
180-
the number of bytes in each chunk with an ``opts`` parameter. Then, it calls
180+
the number of bytes in each chunk in the options parameter. Then, it calls
181181
the ``Write()`` method on the content of ``file.txt`` to write its content to
182182
the stream:
183183

@@ -194,26 +194,26 @@ Retrieve File Information
194194

195195
You can retrieve file metadata stored in the ``files`` collection of the GridFS
196196
bucket. Each document in the ``files`` collection contains the following
197-
information:
197+
pieces of information:
198198

199-
- The file ID
200-
- The file length
201-
- The maximum chunk size
202-
- The upload date and time
203-
- The file name
204-
- A ``metadata`` document in which you can store any other information
199+
- File ID
200+
- File length
201+
- Maximum chunk size
202+
- Upload date and time
203+
- File name
204+
- ``metadata`` document that stores any other information
205205

206206
To retrieve file data, call the ``Find()`` method on a ``GridFSBucket``
207207
instance. You can pass a query filter as an argument to ``Find()`` to match
208208
only certain file documents.
209209

210210
.. note::
211211

212-
The ``Find()`` method requires a query filter as a parameter. To match all
212+
You must pass a query filter to the ``Find()`` method. To retrieve all
213213
documents in the ``files`` collection, pass an empty query filter to ``Find()``.
214214

215-
The following example retrieves the file name and length of documents in the
216-
``files`` collection with ``length`` values greater than ``1500``:
215+
The following example retrieves the file name and length of documents in
216+
which the ``length`` value is greater than ``1500``:
217217

218218
.. code-block:: go
219219

@@ -223,11 +223,12 @@ The following example retrieves the file name and length of documents in the
223223
panic(err)
224224
}
225225

226-
type gridfsFile struct {
226+
type gridFSFile struct {
227227
Name string `bson:"filename"`
228228
Length int64 `bson:"length"`
229229
}
230-
var foundFiles []gridfsFile
230+
231+
var foundFiles []gridFSFile
231232
if err = cursor.All(context.TODO(), &foundFiles); err != nil {
232233
panic(err)
233234
}
@@ -241,35 +242,38 @@ The following example retrieves the file name and length of documents in the
241242
Download Files
242243
~~~~~~~~~~~~~~
243244

244-
You can download a GridFS file in one of the following ways:
245+
You can download a GridFS file by using one of the following methods:
245246

246-
- Use the ``DownloadToStream()`` method to download a file to an output stream.
247-
- Use the ``OpenDownloadStream()`` method to open an input stream.
247+
- ``DownloadToStream()``, which downloads a file to an output stream
248+
- ``OpenDownloadStream()``, which opens an input stream
248249

249250
Download a File to an Output Stream
250251
```````````````````````````````````
251252

252-
You can download a file in a GridFS bucket directly to an output stream using the
253-
``DownloadToStream()`` method. ``DownloadToStream()`` takes a file ID and an
254-
``io.Writer`` as parameters. The method downloads the file with the specified
255-
file ID and writes to the ``io.Writer``.
253+
You can download a file in a GridFS bucket directly to an output stream
254+
by using the ``DownloadToStream()`` method. The ``DownloadToStream()``
255+
method takes a file ID and an ``io.Writer`` instance as parameters. The
256+
method downloads the file with the specified file ID and writes it to the
257+
``io.Writer`` instance.
256258

257259
The following example downloads a file and writes to a file buffer:
258260

259261
.. code-block:: go
260262

261263
id, err := primitive.ObjectIDFromHex("62f7bd54a6e4452da13b3e88")
262264
fileBuffer := bytes.NewBuffer(nil)
265+
263266
if _, err := bucket.DownloadToStream(id, fileBuffer); err != nil {
264267
panic(err)
265268
}
266269

267270
Download a File to an Input Stream
268271
``````````````````````````````````
269272

270-
You can download a file in a GridFS bucket to memory with an input stream using
271-
the ``OpenDownloadStream()`` method. ``OpenDownloadStream()`` takes a file ID as
272-
a parameter and returns an input stream from which you can read the file.
273+
You can download a file in a GridFS bucket to memory with an input
274+
stream by using the ``OpenDownloadStream()`` method. The
275+
``OpenDownloadStream()`` method takes a file ID as a parameter and
276+
returns an input stream from which you can read the file.
273277

274278
The following example downloads a file into memory and reads its contents:
275279

@@ -328,32 +332,35 @@ Delete a GridFS Bucket
328332

329333
You can delete a GridFS bucket by using the ``Drop()`` method.
330334

331-
The following code example deletes a GridFS bucket:
335+
The following code example removes a GridFS bucket:
332336

333337
.. code-block:: go
334338

335339
if err := bucket.Drop(); err != nil {
336340
panic(err)
337341
}
338342

339-
340343
Additional Resources
341344
--------------------
342345

343-
To learn more about GridFS and its operations, visit the :manual:`GridFS manual page </core/gridfs>`.
346+
To learn more about GridFS and storage, see the following pages in the Server
347+
manual:
348+
349+
- :manual:`GridFS </core/gridfs>`
350+
- :manual:`FAQ: MongoDB Storage </faq/storage/>`
344351

345352
API Documentation
346353
~~~~~~~~~~~~~~~~~
347354

348-
To learn more about the methods or types discussed in this guide, see the following
349-
API Documentation:
350-
351-
- `NewBucket() <{+api+}/mongo/gridfs#NewBucket>`__
352-
- `OpenUploadStream() <{+api+}/mongo/gridfs#Bucket.OpenUploadStream>`__
353-
- `UploadFromStream() <{+api+}/mongo/gridfs#Bucket.UploadFromStream>`__
354-
- `Find() <{+api+}/mongo/gridfs#Bucket.Find>`__
355-
- `OpenDownloadStream() <{+api+}/mongo/gridfs#Bucket.OpenUploadStream>`__
356-
- `DownloadToStream() <{+api+}/mongo/gridfs#Bucket.DownloadToStream>`__
357-
- `Rename() <{+api+}/mongo/gridfs#Bucket.Rename>`__
358-
- `Delete() <{+api+}/mongo/gridfs#Bucket.Delete>`__
359-
- `Drop() <{+api+}/mongo/gridfs#Bucket.Drop>`__
355+
To learn more about the methods and types mentioned in this guide, see
356+
the following API documentation:
357+
358+
- `GridFSBucket() <{+api+}/mongo#Database.GridFSBucket>`__
359+
- `OpenUploadStream() <{+api+}/mongo#GridFSBucket.OpenUploadStream>`__
360+
- `UploadFromStream() <{+api+}/mongo#GridFSBucket.UploadFromStream>`__
361+
- `Find() <{+api+}/mongo#GridFSBucket.Find>`__
362+
- `OpenDownloadStream() <{+api+}/mongo#GridFSBucket.OpenDownloadStream>`__
363+
- `DownloadToStream() <{+api+}/mongo#GridFSBucket.DownloadToStream>`__
364+
- `Rename() <{+api+}/mongo#GridFSBucket.Rename>`__
365+
- `Delete() <{+api+}/mongo#GridFSBucket.Delete>`__
366+
- `Drop() <{+api+}/mongo#GridFSBucket.Drop>`__

0 commit comments

Comments
 (0)