Skip to content

Commit 7e64c10

Browse files
authored
DOCSP-42254: Fix GridFS code (#99)
* DOCSP-42254: Fix GridFS code * edit wording * fixes * reword * dedent * RR feedback
1 parent 15d2912 commit 7e64c10

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

source/includes/gridfs/gridfs.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
# start create bucket
2-
const db = client.db(dbName);
3-
const bucket = new mongodb.GridFSBucket(db);
2+
client = MongoClient("<connection string>")
3+
db = client["db"]
4+
bucket = gridfs.GridFSBucket(db)
45
# end create bucket
56

67
# start create custom bucket
7-
const bucket = new mongodb.GridFSBucket(db, { bucketName: 'myCustomBucket' });
8+
custom_bucket = gridfs.GridFSBucket(db, bucket_name="myCustomBucket")
89
# end create custom bucket
910

1011
# start upload files
11-
fs.createReadStream('./myFile').
12-
pipe(bucket.openUploadStream('myFile', {
13-
chunkSizeBytes: 1048576,
14-
metadata: { field: 'myField', value: 'myValue' }
15-
}));
12+
with bucket.open_upload_stream(
13+
"my_file", chunk_size_bytes=1048576, metadata={"contentType": "text/plain"}
14+
) as grid_in:
15+
grid_in.write("data to store")
1616
# end upload files
1717

1818
# start retrieve file info
19-
const cursor = bucket.find({});
20-
for await (const doc of cursor) {
21-
console.log(doc);
22-
}
19+
for file_doc in bucket.find({}):
20+
print(file_doc)
2321
# end retrieve file info
2422

2523
# start download files name
26-
bucket.openDownloadStreamByName('myFile').
27-
pipe(fs.createWriteStream('./outputFile'));
24+
file = bucket.open_download_stream_by_name("my_file")
25+
contents = file.read()
2826
# end download files name
2927

3028
# start download files id
31-
bucket.openDownloadStream(ObjectId("60edece5e06275bf0463aaf3")).
32-
pipe(fs.createWriteStream('./outputFile'));
29+
file = bucket.open_download_stream(ObjectId("66b3c86e672a17b6c8a4a4a9"))
30+
contents = file.read()
3331
# end download files id
3432

3533
# start rename files
36-
bucket.rename(ObjectId("60edece5e06275bf0463aaf3"), "newFileName");
34+
bucket.rename(ObjectId("66b3c86e672a17b6c8a4a4a9"), "new_file_name")
3735
# end rename files
3836

3937
# start delete files
40-
bucket.delete(ObjectId("60edece5e06275bf0463aaf3"));
41-
# end delete files
38+
bucket.delete(ObjectId("66b3c86e672a17b6c8a4a4a9"))
39+
# end delete files

source/write/gridfs.txt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ call read and write operations on the files in your bucket.
8383

8484
.. literalinclude:: /includes/gridfs/gridfs.py
8585
:language: python
86-
:copyable: true
86+
:dedent:
8787
:start-after: start create bucket
8888
:end-before: end create bucket
8989

@@ -95,7 +95,7 @@ constructor, as shown below:
9595

9696
.. literalinclude:: /includes/gridfs/gridfs.py
9797
:language: python
98-
:copyable: true
98+
:dedent:
9999
:start-after: start create custom bucket
100100
:end-before: end create custom bucket
101101

@@ -104,16 +104,16 @@ constructor, as shown below:
104104
Upload Files
105105
------------
106106

107-
Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to create an upload
108-
stream for a given file name. The
109-
``open_upload_stream()`` method allows you to specify configuration information
110-
such as file chunk size and other field/value pairs to store as metadata. Set
111-
these options as parameters of ``open_upload_stream()``, as shown in the
112-
following code example:
107+
Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to
108+
create an upload stream for a given file name. The ``open_upload_stream()``
109+
method allows you to specify configuration information such as file chunk
110+
size and other field/value pairs to store as metadata. Set these options
111+
as parameters of ``open_upload_stream()``, as shown in the following code
112+
example:
113113

114114
.. literalinclude:: /includes/gridfs/gridfs.py
115115
:language: python
116-
:copyable: true
116+
:dedent:
117117
:start-after: start upload files
118118
:end-before: end upload files
119119

@@ -138,12 +138,12 @@ from which you can access the results. To learn more about ``Cursor`` objects in
138138
{+driver-short+}, see :ref:`<pymongo-cursors>`.
139139

140140
The following code example shows you how to retrieve and print file metadata
141-
from all your files in a GridFS bucket. It uses the ``for...of`` syntax to traverse the
141+
from all your files in a GridFS bucket. It uses the ``for...in`` syntax to traverse the
142142
``Cursor`` iterable and display the results:
143143

144144
.. literalinclude:: /includes/gridfs/gridfs.py
145145
:language: python
146-
:copyable: true
146+
:dedent:
147147
:start-after: start retrieve file info
148148
:end-before: end retrieve file info
149149

@@ -162,12 +162,11 @@ You can download files from your MongoDB database by using the
162162
download stream.
163163

164164
The following example shows you how to download a file referenced
165-
by the file name, stored in the ``filename`` field, into your working
166-
directory:
165+
by the file name, ``"my_file"``, and read its contents:
167166

168167
.. literalinclude:: /includes/gridfs/gridfs.py
169168
:language: python
170-
:copyable: true
169+
:dedent:
171170
:start-after: start download files name
172171
:end-before: end download files name
173172

@@ -182,7 +181,7 @@ method, which takes the ``_id`` field of a file as a parameter:
182181

183182
.. literalinclude:: /includes/gridfs/gridfs.py
184183
:language: python
185-
:copyable: true
184+
:dedent:
186185
:start-after: start download files id
187186
:end-before: end download files id
188187

@@ -204,11 +203,11 @@ bucket. You must specify the file to rename by its ``_id`` field
204203
rather than its file name.
205204

206205
The following example shows how to update the ``filename`` field to
207-
``"newFileName"`` by referencing a document's ``_id`` field:
206+
``"new_file_name"`` by referencing a document's ``_id`` field:
208207

209208
.. literalinclude:: /includes/gridfs/gridfs.py
210209
:language: python
211-
:copyable: true
210+
:dedent:
212211
:start-after: start rename files
213212
:end-before: end rename files
214213

@@ -233,7 +232,7 @@ The following example shows you how to delete a file by referencing its ``_id``
233232

234233
.. literalinclude:: /includes/gridfs/gridfs.py
235234
:language: python
236-
:copyable: true
235+
:dedent:
237236
:start-after: start delete files
238237
:end-before: end delete files
239238

0 commit comments

Comments
 (0)