13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

please define productname in your docbook file!class GridFsClient {@AutowiredGridFsOperations operations;@Testpublic void storeFileToGridFs {FileMetadata metadata = new FileMetadata();// populate metadataResource file = … // lookup File or Resource}}operations.store(file.getInputStream(), "filename.txt", metadata);Example 5.29 Using GridFsTemplate to store filesThe store(…) operations take an InputStream, a filename and optionally metadata informationabout the file to store. The metadata can be an arbitrary object which will be marshalled by theMongoConverter configured with the GridFsTemplate. Alternatively you can also provide aDBObject 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 aQuery 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 whereMeta<strong>Data</strong>().class GridFsClient {@AutowiredGridFsOperations operations;}@Testpublic void findFilesInGridFs {List result = operations.find(query(whereFilename().is("filename.txt")))}Example 5.30 Using GridFsTemplate to query for filesNoteCurrently <strong>MongoDB</strong> 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 bedisregarded.The other option to read files from the GridFs is using the methods introduced by theResourcePatternResolver interface. They allow handing an Ant path into the method ar thusretrieve files matching the given pattern.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 68

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!