Skip to content

Commit add6c87

Browse files
committed
DOCSP-45191: GridFS
1 parent 14e1d51 commit add6c87

File tree

3 files changed

+252
-0
lines changed

3 files changed

+252
-0
lines changed
142 KB
Loading

source/includes/write/gridfs.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'bundler/inline'
2+
gemfile do
3+
source 'https://rubygems.org'
4+
gem 'mongo'
5+
end
6+
7+
uri = "<connection string>"
8+
9+
Mongo::Client.new(uri) do |client|
10+
database = client.use('sample_restaurants')
11+
collection = database[:restaurants]
12+
13+
# start-create-bucket
14+
bucket = Mongo::Grid::FSBucket.new(database)
15+
# end-create-bucket
16+
17+
# start-create-custom-bucket
18+
custom_bucket = Mongo::Grid::FSBucket.new(database, bucket_name: 'files')
19+
# end-create-custom-bucket
20+
21+
# start-upload-files
22+
File.open('/path/to/file', 'rb') do |file|
23+
file_id = bucket.upload_from_stream('test.txt', file)
24+
puts "Uploaded file with ID: #{file_id}"
25+
end
26+
# end-upload-files
27+
28+
# start-retrieve-file-info
29+
bucket.find().each do |file|
30+
puts "Filename: #{file[:filename]}"
31+
end
32+
# end-retrieve-file-info
33+
34+
# start-download-files-id
35+
file_id = BSON::ObjectId('your_file_id')
36+
File.open('/path/to/downloaded_file', 'wb') do |file|
37+
bucket.download_to_stream(file_id, file)
38+
end
39+
# end-download-files-id
40+
41+
# start-download-files-name
42+
File.open('/path/to/downloaded_file', 'wb') do |file|
43+
bucket.download_to_stream_by_name('mongodb-tutorial', file)
44+
end
45+
# end-download-files-name
46+
47+
# start-delete-files
48+
file_id = BSON::ObjectId('your_file_id')
49+
bucket.delete(file_id)
50+
# end-delete-files
51+
end

source/write/gridfs.txt

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
.. _ruby-gridfs:
2+
3+
=================================
4+
Store Large Files by Using GridFS
5+
=================================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: binary large object, blob, storage
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to store and retrieve large files in
24+
MongoDB by using **GridFS**. GridFS is a specification that describes how to split files
25+
into chunks when storing them and reassemble them when retrieving them. The {+driver-short+}'s
26+
implementation of GridFS is an abstraction that manages the operations and organization of
27+
the file storage.
28+
29+
Use GridFS if the size of your files exceeds the BSON document
30+
size limit of 16MB. For more detailed information on whether GridFS is
31+
suitable for your use case, see :manual:`GridFS </core/gridfs>` in the
32+
{+mdb-server+} manual.
33+
34+
The following sections describe GridFS operations and how to
35+
perform them.
36+
37+
How GridFS Works
38+
----------------
39+
40+
GridFS organizes files in a **bucket**, a group of MongoDB collections
41+
that contain the chunks of files and information describing them. The
42+
bucket contains the following collections, named using the convention
43+
defined in the GridFS specification:
44+
45+
- The ``chunks`` collection stores the binary file chunks.
46+
- The ``files`` collection stores the file metadata.
47+
48+
When you create a new GridFS bucket, the driver creates the ``fs.chunks`` and ``fs.files``
49+
collections, unless you specify a different name in the ``Grid::FSBucket.new`` method options. The
50+
driver also creates an index on each collection to ensure efficient retrieval of the files and related
51+
metadata. The driver creates the GridFS bucket, if it doesn't exist, only when the first write
52+
operation is performed. The driver creates indexes only if they don't exist and when the
53+
bucket is empty. For more information about
54+
GridFS indexes, see :manual:`GridFS Indexes </core/gridfs/#gridfs-indexes>`
55+
in the {+mdb-server+} manual.
56+
57+
When storing files with GridFS, the driver splits the files into smaller
58+
chunks, each represented by a separate document in the ``chunks`` collection.
59+
It also creates a document in the ``files`` collection that contains
60+
a file ID, file name, and other file metadata. You can upload the file from
61+
memory or from a stream. See the following diagram to see how GridFS splits
62+
the files when uploaded to a bucket.
63+
64+
.. figure:: /includes/figures/GridFS-upload.png
65+
:alt: A diagram that shows how GridFS uploads a file to a bucket
66+
67+
When retrieving files, GridFS fetches the metadata from the ``files``
68+
collection in the specified bucket and uses the information to reconstruct
69+
the file from documents in the ``chunks`` collection. You can read the file
70+
into memory or output it to a stream.
71+
72+
Create a GridFS Bucket
73+
----------------------
74+
75+
To store or retrieve files from GridFS, create a GridFS bucket by calling the
76+
``FSBucket.new`` method and passing in a ``Mongo::Database`` instance.
77+
You can use the ``FSBucket`` instance to
78+
call read and write operations on the files in your bucket.
79+
80+
.. literalinclude:: /includes/write/gridfs.rb
81+
:language: ruby
82+
:dedent:
83+
:start-after: start-create-bucket
84+
:end-before: end-create-bucket
85+
86+
To create or reference a bucket with a custom name other than the default name
87+
``fs``, pass the bucket name as the an optional parameter to the ``FSBucket.new``
88+
constructor, as shown in the following example:
89+
90+
.. literalinclude:: /includes/write/gridfs.rb
91+
:language: ruby
92+
:dedent:
93+
:start-after: start-create-custom-bucket
94+
:end-before: end-create-custom-bucket
95+
96+
Upload Files
97+
------------
98+
99+
The ``upload_from_stream`` method reads the contents of an
100+
upload stream and saves it to the ``GridFSBucket`` instance.
101+
102+
You can pass a ``Hash`` as an optional parameter to configure the chunk size or include
103+
additional metadata.
104+
105+
The following example uploads a file into ``FSBucket``:
106+
107+
.. literalinclude:: /includes/write/gridfs.rb
108+
:language: ruby
109+
:dedent:
110+
:start-after: start-upload-files
111+
:end-before: end-upload-files
112+
113+
Retrieve File Information
114+
-------------------------
115+
116+
In this section, you can learn how to retrieve file metadata stored in the
117+
``files`` collection of the GridFS bucket. The metadata contains information
118+
about the file it refers to, including:
119+
120+
- The ``_id`` of the file
121+
- The name of the file
122+
- The length/size of the file
123+
- The upload date and time
124+
- A ``metadata`` document in which you can store any other information
125+
126+
To learn more about fields you can retrieve from the ``files`` collection, see the
127+
:manual:`GridFS Files Collection </core/gridfs/#the-files-collection>` documentation in the
128+
{+mdb-server+} manual.
129+
130+
To retrieve files from a GridFS bucket, call the ``find`` method on the ``FSBucket``
131+
instance. The following code example retrieves and prints file metadata from all files in
132+
a GridFS bucket:
133+
134+
.. literalinclude:: /includes/write/gridfs.rb
135+
:language: ruby
136+
:dedent:
137+
:start-after: start-retrieve-file-info
138+
:end-before: end-retrieve-file-info
139+
140+
To learn more about querying MongoDB, see :ref:`<ruby-retrieve>`.
141+
142+
Download Files
143+
--------------
144+
145+
The ``download_to_stream`` method downloads the contents of a file.
146+
147+
To download a file by its file ``_id``, pass the ``_id`` to the method. The ``download_to_stream``
148+
method will write the contents of the file to the provided object.
149+
The following example downloads a file by its file ``_id``:
150+
151+
.. literalinclude:: /includes/write/gridfs.rb
152+
:language: ruby
153+
:dedent:
154+
:start-after: start-download-files-id
155+
:end-before: end-download-files-id
156+
157+
If you don't know the ``_id`` of the file but know the filename, then you
158+
can use the ``download_to_stream_by_name`` method. The following example
159+
downloads a file named ``mongodb-tutorial``:
160+
161+
.. literalinclude:: /includes/write/gridfs.rb
162+
:language: ruby
163+
:dedent:
164+
:start-after: start-download-files-name
165+
:end-before: end-download-files-name
166+
167+
.. note::
168+
169+
If there are multiple documents with the same ``filename`` value,
170+
GridFS will fetch the most recent file with the given name (as
171+
determined by the ``uploadDate`` field).
172+
173+
Delete Files
174+
------------
175+
176+
Use the ``delete`` method to remove a file's collection document and associated
177+
chunks from your bucket. You must specify the file by its ``_id`` field rather than its
178+
file name.
179+
180+
The following example deletes a file by its ``_id``:
181+
182+
.. literalinclude:: /includes/write/gridfs.rb
183+
:language: ruby
184+
:dedent:
185+
:start-after: start-delete-files
186+
:end-before: end-delete-files
187+
188+
.. note::
189+
190+
The ``delete`` method supports deleting only one file at a time. To
191+
delete multiple files, retrieve the files from the bucket, extract
192+
the ``_id`` field from the files you want to delete, and pass each value
193+
in separate calls to the ``delete`` method.
194+
195+
API Documentation
196+
-----------------
197+
198+
To learn more about using GridFS to store and retrieve large files,
199+
see the following API documentation:
200+
201+
- `Mongo::Grid::FSBucket <{+api-root+}/Mongo/Grid/FSBucket.html>`_

0 commit comments

Comments
 (0)