Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions source/includes/gridfs/gridfs.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
# start create bucket
const db = client.db(dbName);
const bucket = new mongodb.GridFSBucket(db);
# end create bucket
# start create bucket
client = MongoClient("<connection string>")
db = client["db"]
bucket = gridfs.GridFSBucket(db)
# end create bucket

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: why is the code in this file indented?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

# start create custom bucket
const bucket = new mongodb.GridFSBucket(db, { bucketName: 'myCustomBucket' });
# end create custom bucket
# start create custom bucket
custom_bucket = gridfs.GridFSBucket(db, bucket_name="myCustomBucket")
# end create custom bucket

# start upload files
fs.createReadStream('./myFile').
pipe(bucket.openUploadStream('myFile', {
chunkSizeBytes: 1048576,
metadata: { field: 'myField', value: 'myValue' }
}));
# end upload files
# start upload files
with bucket.open_upload_stream(
"my_file", chunk_size_bytes=1048576,
metadata={"contentType": "text/plain"}) as grid_in:
grid_in.write("data to store")
# end upload files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: i think the line formatting needs to be corrected here


# start retrieve file info
const cursor = bucket.find({});
for await (const doc of cursor) {
console.log(doc);
}
# end retrieve file info
# start retrieve file info
for file_doc in bucket.find({}):
print(file_doc)
# end retrieve file info

# start download files name
bucket.openDownloadStreamByName('myFile').
pipe(fs.createWriteStream('./outputFile'));
# end download files name
# start download files name
file = bucket.open_download_stream_by_name("my_file")
contents = file.read()
# end download files name

# start download files id
bucket.openDownloadStream(ObjectId("60edece5e06275bf0463aaf3")).
pipe(fs.createWriteStream('./outputFile'));
# end download files id
# start download files id
file = bucket.open_download_stream(ObjectId('66b3c86e672a17b6c8a4a4a9'))
contents = file.read()
# end download files id

# start rename files
bucket.rename(ObjectId("60edece5e06275bf0463aaf3"), "newFileName");
# end rename files
# start rename files
bucket.rename(ObjectId('66b3c86e672a17b6c8a4a4a9'), "new_file_name")
# end rename files

# start delete files
bucket.delete(ObjectId("60edece5e06275bf0463aaf3"));
# end delete files
# start delete files
bucket.delete(ObjectId('66b3c86e672a17b6c8a4a4a9'))
# end delete files
35 changes: 17 additions & 18 deletions source/write/gridfs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ call read and write operations on the files in your bucket.

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start create bucket
:end-before: end create bucket

Expand All @@ -95,7 +95,7 @@ constructor, as shown below:

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start create custom bucket
:end-before: end create custom bucket

Expand All @@ -104,16 +104,16 @@ constructor, as shown below:
Upload Files
------------

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

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start upload files
:end-before: end upload files

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

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

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start retrieve file info
:end-before: end retrieve file info

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

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

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start download files name
:end-before: end download files name

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

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start download files id
:end-before: end download files id

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

The following example shows how to update the ``filename`` field to
``"newFileName"`` by referencing a document's ``_id`` field:
``"new_file_name"`` by referencing a document's ``_id`` field:

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start rename files
:end-before: end rename files

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

.. literalinclude:: /includes/gridfs/gridfs.py
:language: python
:copyable: true
:dedent:
:start-after: start delete files
:end-before: end delete files

Expand Down
Loading