You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/gridfs/deletingandrenamingfiles.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
+++
2
-
date = "2015-09-41T00:00:00Z"
2
+
date = "2015-09-14T00:00:00Z"
3
3
draft = false
4
4
title = "Deleting and Renaming Files"
5
5
[menu.main]
@@ -15,9 +15,9 @@ These methods allow you to delete or rename GridFS files.
15
15
16
16
### Deleting a single file
17
17
18
-
The DeleteAsync method is used to delete a single file identified by its Id.
18
+
The [`DeleteAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_DeleteAsync_1" >}}) method is used to delete a single file identified by its Id.
19
19
20
-
```
20
+
```csharp
21
21
IGridFSBucketbucket;
22
22
ObjectIdid;
23
23
@@ -26,21 +26,21 @@ await bucket.DeleteAsync(id);
26
26
27
27
### Dropping an entire GridFS bucket
28
28
29
-
If you want to drop an entire GridFS bucket at once use the DropAsync method.
29
+
If you want to drop an entire GridFS bucket at once use the [`DropAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_DropAsync" >}}) method.
30
30
31
-
```
31
+
```csharp
32
32
IGridFSBucketbucket;
33
33
34
34
awaitbucket.DropAsync();
35
35
```
36
36
37
-
The "fs.files" collection will be dropped first, followed by the "fs.chunks" collection. This is the fastest way to delete all files stored in a GridFS bucket at once.
37
+
{{% note %}}The "fs.files" collection will be dropped first, followed by the "fs.chunks" collection. This is the fastest way to delete all files stored in a GridFS bucket at once.{{% /note %}}
38
38
39
39
### Renaming a single file
40
40
41
-
The RenameAsync method is used to rename a single file identified by its Id.
41
+
The [`RenameAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_RenameAsync_1" >}}) method is used to rename a single file identified by its Id.
If you want to rename all revisions of a file you first use FindAsyncto find their ids and then call RenameAsync in a loop to rename them one at a time.
53
+
If you want to rename all revisions of a file you first use [`FindAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_FindAsync" >}}) to find their ids and then call [`RenameAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_RenameAsync_1" >}}) in a loop to rename them one at a time.
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/gridfs/downloadingfiles.md
+20-18Lines changed: 20 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
+++
2
-
date = "2015-09-41T00:00:00Z"
2
+
date = "2015-09-14T00:00:00Z"
3
3
draft = false
4
4
title = "Downloading Files"
5
5
[menu.main]
@@ -13,14 +13,14 @@ title = "Downloading Files"
13
13
14
14
There are several ways to download a file from GridFS. The two main approaches are:
15
15
16
-
1. The driver downloads a file as a byte array or by writing the contents to a Stream provided by the application
17
-
2. The driver supplies a Stream object that the application can read the contents from
16
+
1. The driver downloads a file as a byte array or by writing the contents to a [`Stream`]({{< msdnref "system.io.stream" >}}) provided by the application
17
+
2. The driver supplies a [`Stream`]({{< msdnref "system.io.stream" >}}) object that the application can read the contents from
18
18
19
19
### Downloading as a byte array
20
20
21
21
This is the easiest way to download a file from GridFS, assuming that the file is small enough for the entire contents to be held in memory at once.
22
22
23
-
```
23
+
```csharp
24
24
IGridFSBucketbucket;
25
25
ObjectIdid;
26
26
@@ -29,23 +29,25 @@ var bytes = await bucket.DownloadAsBytesAsync(id);
29
29
30
30
### Downloading to a Stream
31
31
32
-
If you don't want to hold the entire contents of the downloaded file in memory at once, you can have the driver write the contents of the file to a Stream provided by the application.
32
+
If you don't want to hold the entire contents of the downloaded file in memory at once, you can have the driver write the contents of the file to a [`Stream`]({{< msdnref "system.io.stream" >}}) provided by the application.
The driver will download the contents of the GridFS file and write them to the destination Stream. The driver begins writing the contents at the current position of the Stream, and does **not** close the Stream when it is done. The Stream is owned by the application and it is up to the application to close the Stream when it is ready to do so.
42
+
The driver will download the contents of the GridFS file and write them to the destination [`Stream`]({{< msdnref "system.io.stream" >}}). The driver begins writing the contents at the current position of the [`Stream`]({{< msdnref "system.io.stream" >}}).
43
+
44
+
{{% note class="important" %}}The driver does **not** close the [`Stream`]({{< msdnref "system.io.stream" >}}) when it is done. The [`Stream`]({{< msdnref "system.io.stream" >}}) is owned by the application and it is up to the application to close the [`Stream`]({{< msdnref "system.io.stream" >}}) when it is ready to do so.{{% /note %}}
43
45
44
46
### Downloading from a Stream
45
47
46
-
In some cases it the application might prefer to read the contents of the GridFS file from a Stream.
48
+
In some cases the application might prefer to read the contents of the GridFS file from a [`Stream`]({{< msdnref "system.io.stream" >}}).
47
49
48
-
```
50
+
```csharp
49
51
IGridFSBucketbucket;
50
52
ObjectIdid;
51
53
@@ -56,25 +58,25 @@ using (var stream = await bucket.OpenDownloadStreamAsync(id))
56
58
}
57
59
```
58
60
59
-
The Streamobject returned by OpenDownloadStreamAsyncis actually a GridFSDownloadStream(a subclass of Stream), which has the following additional members in addition to those found in Stream:
61
+
The [`Stream`]({{< msdnref "system.io.stream" >}}) object returned by [`OpenDownloadStreamAsync`]({{< apiref "M_MongoDB_Driver_GridFS_OpenDownloadStreamAsync_1." >}}) is actually a [`GridFSDownloadStream`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSDownloadStream" >}}) (a subclass of [`Stream`]({{< msdnref "system.io.stream" >}})), which has the following additional members in addition to those found in [`Stream`]({{< msdnref "system.io.stream" >}}):
The FileInfoproperty contains information about the GridFS file being dowloaded. See the FindAsyncmethod for details about the GridFSFileInfo class.
71
+
The [`FileInfo`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSDownloadStream_FileInfo" >}}) property contains information about the GridFS file being dowloaded. See the [`FindAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_FindAsync" >}}) method for details about the [`GridFSFileInfo`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSFileInfo" >}}) class.
70
72
71
-
Calling CloseAsyncis optional, but recommended. Since Streamis IDisposableand it is used inside a using statement, it would be closed automatically when Disposeis called. However, in async programming we want to avoid blocking and calling CloseAsyncfirst allows the Streamto be closed with an async call. If you call CloseAsyncfirst then Disposewill no longer block.
73
+
{{% note %}}Calling [`CloseAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSDownloadStream_CloseAsync." >}}) is optional, but recommended. Since [`Stream`]({{< msdnref "system.io.stream" >}}) is [`IDisposable`]({{< msdnref "system.idisposable" >}}) and it is used inside a using statement, it would be closed automatically when [`Dispose`]({{< msdnref "system.idisposable.dispose" >}}) is called. However, in async programming we want to avoid blocking and calling [`CloseAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSDownloadStream_CloseAsync." >}}) first allows the [`Stream`]({{< msdnref "system.io.stream" >}}) to be closed with an async call. If you call [`CloseAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSDownloadStream_CloseAsync." >}}) first then [`Dispose`]({{< msdnref "system.idisposable.dispose" >}}) will no longer block.{{% /note %}}
72
74
73
-
By default the driver assumes that you want to read the entire contents of the file from beginning to end, and returns a Stream implementation that does not support seeking, which allows for a more efficient implementation.
75
+
By default the driver assumes that you want to read the entire contents of the file from beginning to end, and returns a [`Stream`]({{< msdnref "system.io.stream" >}}) implementation that does not support seeking, which allows for a more efficient implementation.
74
76
75
-
If you do want to use Seekwith the returned Stream, you can use the options parameter to indicate that.
77
+
If you do want to use [`Seek`]({{< msdnref "system.io.stream.seek" >}}) with the returned [`Stream`]({{< msdnref "system.io.stream" >}}), you can use the options parameter to indicate that.
76
78
77
-
```
79
+
```csharp
78
80
IGridFSBucketbucket;
79
81
ObjectIdid;
80
82
@@ -107,7 +109,7 @@ The default value for the revision is -1 (i.e. the newest revision).
107
109
108
110
The following examples all download the newest revision:
109
111
110
-
```
112
+
```csharp
111
113
IGridFSBucketbucket;
112
114
stringfilename;
113
115
@@ -129,7 +131,7 @@ using (var stream = await bucket.OpenDownloadStreamByNameAsync(filename))
129
131
130
132
If you want to download a different revision, you specify the desired revision using the options parameter.
@@ -15,11 +15,11 @@ Each file stored in GridFS has a unique Id assigned to it, and that is the prima
15
15
16
16
### FindAsync method
17
17
18
-
If you don't know the Id, you can use FindAsyncto find matching files using a filter. The filter must be of type FilterDefinition<GridFSFileInfo>.
18
+
If you don't know the Id, you can use [`FindAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_FindAsync" >}}) to find matching files using a filter. The filter must be of type [`FilterDefinition<GridFSFileInfo>`]({{< apiref "T_MongoDB_Driver_FilterDefinition_1" >}}).
19
19
20
-
For example, to find the newest revision of the file named "securityvideo" uploaded in January 2015 we could use FindAsync like this:
20
+
For example, to find the newest revision of the file named "securityvideo" uploaded in January 2015 we could use [`FindAsync`]({{< apiref "M_MongoDB_Driver_GridFS_GridFSBucket_FindAsync" >}}) like this:
The GridFSFileInfo is a strongly typed class that represents the information about a GridFS file stored in the "fs.files" collection.
45
-
46
-
It is defined as:
47
-
48
-
```
49
-
public class GridFSFileInfo
50
-
{
51
-
public BsonDocument BackingDocument { get; }
52
-
public int ChunkSizeBytes { get; }
53
-
public string Filename { get; }
54
-
public ObjectId Id { get; }
55
-
public long Length { get; }
56
-
public string MD5 { get; }
57
-
public BsonDocument Metadata { get; }
58
-
public DateTime UploadDateTime { get; }
59
-
60
-
// the following are deprecated but kept for backward compatibility
61
-
public IEnumerable<string> Aliases { get; }
62
-
public string ContentType { get; }
63
-
public BsonValue IdAsBsonValue { get; }
64
-
}
65
-
```
44
+
The [`GridFSFileInfo`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSFileInfo" >}}) is a strongly typed class that represents the information about a GridFS file stored in the "fs.files" collection.
66
45
67
-
This class is a strongly typed wrapper around a backing BsonDocument. It makes it easier to extract the information available in a files collection documents.
46
+
This class is a strongly typed wrapper around a backing [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). It makes it easier to extract the information available in a files collection documents.
68
47
69
-
In older drivers it was possible to store arbitrary information at the root level of a files collection document. If you need to access that information you can use the BackingDocumentproperty to get access to the complete backing document. When uploading new GridFS files you should store any additional information you want to associate with the uploaded file inside the Metadata document.
48
+
In older drivers it was possible to store arbitrary information at the root level of a files collection document. If you need to access that information you can use the [`BackingDocument`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSFileInfo_BackingDocument" >}}) property to get access to the complete backing document. When uploading new GridFS files you should store any additional information you want to associate with the uploaded file inside the [`Metadata`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSFileInfo_Metadata" >}}) document.
GridFS files are stored in the database using two collections, normally called "fs.files" and "fs.chunks". Each file uploaded to GridFS has one document in the "fs.files" containing information about the file and as many chunks as necessary in the "fs.chunks" collection to store the contents of the file.
14
+
GridFS files are stored in the database using two collections, normally called "fs.files" and "fs.chunks". Each file uploaded to GridFS has one document in the "fs.files" collection containing information about the file and as many chunks as necessary in the "fs.chunks" collection to store the contents of the file.
15
15
16
16
A GridFS "bucket" is the combination of an "fs.files" and "fs.chunks" collection which together represent a bucket where GridFS files can be stored.
17
17
18
18
### GridFSBucket
19
19
20
-
A GridFSBucketobject is the root object representing a GridFS bucket. You should always use a GridFSBucket object to interact with GridFS instead of directly referencing the underlying collections.
20
+
A [`GridFSBucket`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSBucket" >}}) object is the root object representing a GridFS bucket.
21
21
22
-
You create a GridFSBucket instance by calling its constructor:
22
+
{{% note class="warning" %}}You should always use a [`GridFSBucket`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSBucket" >}}) object to interact with GridFS instead of directly referencing the underlying collections.{{% /note %}}
23
23
24
-
```
24
+
You create a [`GridFSBucket`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSBucket" >}}) instance by calling its constructor:
25
+
26
+
```csharp
25
27
IMongoDatabasedatabase;
26
28
27
29
varbucket=newGridFSBucket(database);
28
30
```
29
31
30
-
You can also provide options when instantiating the GridFSBucket object:
32
+
You can also provide options when instantiating the [`GridFSBucket`]({{< apiref "T_MongoDB_Driver_GridFS_GridFSBucket" >}}) object:
@@ -41,8 +43,8 @@ var bucket = new GridFSBucket(database, new GridFSOptions
41
43
});
42
44
```
43
45
44
-
The BucketName value is the root part of the files and chunks collection names, so in this example the two collections would be named "videos.files" and "videos.chunks" instead of "fs.files" and "fs.chunks".
46
+
The [`BucketName`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSBucketOptions_BucketName" >}}) value is the root part of the files and chunks collection names, so in this example the two collections would be named "videos.files" and "videos.chunks" instead of "fs.files" and "fs.chunks".
45
47
46
-
The ChunkSizeBytes value defines the size of each chunk, and in this example we are overriding the default value of 261120 (255MB).
48
+
The [`ChunkSizeBytes`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSBucketOptions_ChunkSizeBytes" >}}) value defines the size of each chunk, and in this example we are overriding the default value of 261120 (255MB).
47
49
48
-
The WriteConcernis used when uploading files to GridFS, and the ReadPreference is used when downloading files from GridFS.
50
+
The [`WriteConcern`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSBucketOptions_WriteConcern" >}}) is used when uploading files to GridFS, and the [`ReadPreference`]({{< apiref "P_MongoDB_Driver_GridFS_GridFSBucketOptions_ReadPreference" >}}) is used when downloading files from GridFS.
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/gridfs/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ title = "GridFS"
11
11
12
12
## GridFS
13
13
14
-
GridFS is a way of storing binary information that is larger than the maximum document size (currently 16MB). When you upload a file to GridFS the file is broken into chunks and the individual chunks are uploaded. When you download a file from GridFS the original content is reassembled from the chunks.
14
+
GridFS is a way of storing binary information larger than the maximum document size (currently 16MB). When you upload a file to GridFS the file is broken into chunks and the individual chunks are uploaded. When you download a file from GridFS the original content is reassembled from the chunks.
0 commit comments