Skip to content

Commit 01d8421

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1855 - Documentation.
1 parent 87f1f16 commit 01d8421

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
== What's New in Spring Data MongoDB 2.2
66
* <<mongo.query.kotlin-support,Type-safe Queries for Kotlin>>
77
* <<mongodb.reactive.repositories.queries.type-safe,Querydsl support for reactive repositories>> via `ReactiveQuerydslPredicateExecutor`.
8+
* <<reactive.gridfs,Reactive GridFS support>>.
89

910
[[new-features.2-1-0]]
1011
== What's New in Spring Data MongoDB 2.1

src/main/asciidoc/reference/reactive-mongodb.adoc

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,96 @@ Flux<Boolean> hasIndex = operations.execute("geolocation",
482482
.flatMap(document -> Mono.just(true))
483483
.defaultIfEmpty(false));
484484
----
485+
486+
[[reactive.gridfs]]
487+
== GridFS Support
488+
489+
MongoDB supports storing binary files inside its filesystem, GridFS.
490+
Spring Data MongoDB provides a `ReactiveGridFsOperations` interface as well as the corresponding implementation, `ReactiveGridFsTemplate`, to let you interact with the filesystem.
491+
You can set up a `ReactiveGridFsTemplate` instance by handing it a `ReactiveMongoDatabaseFactory` as well as a `MongoConverter`, as the following example shows:
492+
493+
.JavaConfig setup for a ReactiveGridFsTemplate
494+
====
495+
[source,java]
496+
----
497+
class GridFsConfiguration extends AbstractReactiveMongoConfiguration {
498+
499+
// … further configuration omitted
500+
501+
@Bean
502+
public ReactiveGridFsTemplate reactiveGridFsTemplate() {
503+
return new ReactiveGridFsTemplate(reactiveMongoDbFactory(), mappingMongoConverter());
504+
}
505+
}
506+
----
507+
====
508+
509+
The template can now be injected and used to perform storage and retrieval operations, as the following example shows:
510+
511+
.Using ReactiveGridFsTemplate to store files
512+
====
513+
[source,java]
514+
----
515+
class ReactiveGridFsClient {
516+
517+
@Autowired
518+
ReactiveGridFsTemplate operations;
519+
520+
@Test
521+
public Mono<ObjectId> storeFileToGridFs() {
522+
523+
FileMetadata metadata = new FileMetadata();
524+
// populate metadata
525+
Publisher<DataBuffer> file = … // lookup File or Resource
526+
527+
return operations.store(file, "filename.txt", metadata);
528+
}
529+
}
530+
----
531+
====
532+
533+
The `store(…)` operations take an `Publisher<DataBuffer>`, a filename, and (optionally) metadata information about the file to store. The metadata can be an arbitrary object, which will be marshaled by the `MongoConverter` configured with the `ReactiveGridFsTemplate`. Alternatively, you can also provide a `Document`.
534+
535+
NOTE: MongoDB's driver uses `AsyncInputStream` and `AsyncOutputStream` interfaces to exchange binary streams. Spring Data MongoDB adapts these interfaces to `Publisher<DataBuffer>`. Read more about `DataBuffer` in http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/core.html#databuffers[Spring's reference documentation].
536+
537+
You can read files from the filesystem through either the `find(…)` or the `getResources(…)` methods. Let's have a look at the `find(…)` methods first. You can either find a single file or multiple files that match a `Query`. You can use the `GridFsCriteria` helper class to define queries. It provides static factory methods to encapsulate default metadata fields (such as `whereFilename()` and `whereContentType()`) or a custom one through `whereMetaData()`. The following example shows how to use `ReactiveGridFsTemplate` to query for files:
538+
539+
.Using ReactiveGridFsTemplate to query for files
540+
====
541+
[source,java]
542+
----
543+
class ReactiveGridFsClient {
544+
545+
@Autowired
546+
ReactiveGridFsTemplate operations;
547+
548+
@Test
549+
public Flux<GridFSFile> findFilesInGridFs() {
550+
return operations.find(query(whereFilename().is("filename.txt")))
551+
}
552+
}
553+
----
554+
====
555+
556+
NOTE: Currently, MongoDB does not support defining sort criteria when retrieving files from GridFS. For this reason, any sort criteria defined on the `Query` instance handed into the `find(…)` method are disregarded.
557+
558+
The other option to read files from the GridFs is to use the methods modeled along the lines of `ResourcePatternResolver`.
559+
`ReactiveGridFsOperations` uses reactive types to defer execution while `ResourcePatternResolver` uses a synchronous interface.
560+
These methods allow handing an Ant path into the method and can thus retrieve files matching the given pattern. The following example shows how to use `ReactiveGridFsTemplate` to read files:
561+
562+
.Using ReactiveGridFsTemplate to read files
563+
====
564+
[source,java]
565+
----
566+
class ReactiveGridFsClient {
567+
568+
@Autowired
569+
ReactiveGridFsOperations operations;
570+
571+
@Test
572+
public void readFilesFromGridFs() {
573+
Flux<ReactiveGridFsResource> txtFiles = operations.getResources("*.txt");
574+
}
575+
}
576+
----
577+
====

0 commit comments

Comments
 (0)