diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java
index e465711cf..bac21f6f9 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java
@@ -19,6 +19,7 @@ import java.io.InputStream;
import java.util.List;
import org.springframework.core.io.support.ResourcePatternResolver;
+import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.DBObject;
@@ -63,8 +64,10 @@ public interface GridFsOperations extends ResourcePatternResolver {
GridFSFile store(InputStream content, String filename, DBObject metadata);
/**
- * Returns all files matching the given query.
+ * Returns all files matching the given query. Note, that currently {@link Sort} criterias defined at the
+ * {@link Query} will not be regarded as MongoDB does not support ordering for GridFS file access.
*
+ * @see https://jira.mongodb.org/browse/JAVA-431
* @param query
* @return
*/
@@ -102,4 +105,4 @@ public interface GridFsOperations extends ResourcePatternResolver {
* @see ResourcePatternResolver#getResources(String)
*/
GridFsResource[] getResources(String filenamePattern);
-}
\ No newline at end of file
+}
diff --git a/spring-data-mongodb/src/test/resources/gridfs/gridfs.xml b/spring-data-mongodb/src/test/resources/gridfs/gridfs.xml
index 4fef73f55..274878ec2 100644
--- a/spring-data-mongodb/src/test/resources/gridfs/gridfs.xml
+++ b/spring-data-mongodb/src/test/resources/gridfs/gridfs.xml
@@ -5,11 +5,11 @@
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
-
-
+
+
-
+
diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml
index 3228426c7..7b914d001 100644
--- a/src/docbkx/reference/mongodb.xml
+++ b/src/docbkx/reference/mongodb.xml
@@ -2731,4 +2731,147 @@ mongoTemplate.dropCollection("MyNewCollection");
}
});
+
+
+ GridFS support
+
+ MongoDB supports storing binary files inside it's filesystem GridFS.
+ Spring Data MongoDB provides a
+ GridFsOperations interface as well as the
+ according implementation GridFsTemplate to easily
+ interact with the filesystem. You can setup a
+ GridFsTemplate instance by handing it a
+ MongoDbFactory as well as a
+ MongoConverter:
+
+
+ JavaConfig setup for a GridFsTemplate
+
+ class GridFsConfiguration extends AbstractMongoConfiguration {
+
+ // … further configuration omitted
+
+ @Bean
+ public GridFsTemplate gridFsTemplate() {
+ return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
+ }
+}
+
+
+ An according XML configuration looks like this:
+
+
+ XML configuration for a GridFsTemplate
+
+ <?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:mongo="http://www.springframework.org/schema/data/mongo"
+ xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
+ http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
+ http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <mongo:db-factory id="mongoDbFactory" dbname="database" />
+ <mongo:mapping-converter id="converter" />
+
+ <bean class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
+ <constructor-arg ref="mongoDbFactory" />
+ <constructor-arg ref="converter" />
+ </bean>
+
+</beans>
+
+
+ You can no get the template injected and perform storing and
+ retrieving operations to it.
+
+
+ Using GridFsTemplate to store files
+
+ class GridFsClient {
+
+ @Autowired
+ GridFsOperations operations;
+
+ @Test
+ public void storeFileToGridFs {
+
+ FileMetadata metadata = new FileMetadata();
+ // populate metadata
+ Resource file = … // lookup File or Resource
+
+ operations.store(file.getInputStream(), "filename.txt", metadata);
+ }
+}
+
+
+ The store(…) operations take an
+ InputStream, a filename and optionally
+ metadata information about the file to store. The metadata can be an
+ arbitrary object which will be marshalled by the
+ MongoConverter configured with the
+ GridFsTemplate. Alternatively you can also provide
+ a DBObject as well.
+
+ Reading files from the filesystem can either be achieved through the
+ find(…) or
+ getResources(…) methods. Let's have a look at the
+ find(…) methods first. You can either find a
+ single file matching a Query or multiple ones. To
+ easily define file queries we provide the
+ GridFsCriteria helper class. It provides static
+ factory methods to encapsulate default metadata fields (e.g.
+ whereFilename(),
+ whereContentType()) or the custom one through
+ whereMetaData().
+
+
+ Using GridFsTemplate to query for files
+
+ class GridFsClient {
+
+ @Autowired
+ GridFsOperations operations;
+
+ @Test
+ public void findFilesInGridFs {
+ List<GridFSDBFile> result = operations.find(query(whereFilename().is("filename.txt")))
+ }
+}
+
+
+
+ Currently MongoDB does not support defining sort criterias when
+ retrieving files from GridFS. Thus any sort criterias defined on the
+ Query instance handed into the
+ find(…) method will be disregarded.
+
+
+ The other option to read files from the GridFs is using the methods
+ introduced by the ResourcePatternResolver
+ interface. They allow handing an Ant path into the method ar thus retrieve
+ files matching the given pattern.
+
+
+ Using GridFsTemplate to read files
+
+ class GridFsClient {
+
+ @Autowired
+ GridFsOperations operations;
+
+ @Test
+ public void readFilesFromGridFs {
+ GridFsResources[] txtFiles = operations.getResources("*.txt");
+ }
+}
+
+
+ GridFsOperations extending
+ ResourcePatternResolver allows the
+ GridFsTemplate e.g. to be plugged into an
+ ApplicationContext to read Spring Config
+ files from a MongoDB.
+