Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for paging and sorting on GridFSTemplate [DATAMONGO-765] #1695

Closed
spring-projects-issues opened this issue Sep 27, 2013 · 6 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Benjamin M opened DATAMONGO-765 and commented

I wondered why GridFsOperations' find methods won't return Page<...>. Or is there another way to get this done?

Additionally I tried to to pass a Pageable object into my Query which doesn't work. Also the limit(...) method and with(new Sort(...)) methods won't work.

Here's my code:

List<GridFSFBFile> files = operations.find(new Query().limit(5));
List<GridFSDBFile> files = operations.find(new Query().with(pageable));
List<GridFSDBFile> files = operations.find(new Query().with(new Sort(Direction.DESC, "uploadDate")));
List<GridFSDBFile> files = operations.find(new Query().with(new Sort(new Sort.Order(Direction.ASC, "uploadDate"))));

All of those queries return the same objects in the same order


Affects: 1.3.1

Issue Links:

  • DATAMONGO-2411 gridFsTemplate does not consider skip/limit parameters
    ("is duplicated by")
  • DATAMONGO-1119 Querying GridFS gives exception under certain circumstances
    ("supersedes")

Referenced from: pull request #806

3 votes, 8 watchers

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

The reason for this is quite simple: currently we're using the Java driver's GridFS instance directly and it doesn't expose the necessary API needed for paging (counting the files matching a query).

After a brief look I've noticed that GridFS seems to be quite open for extension, so we could theoretically subclass it and add the necessary methods

@spring-projects-issues
Copy link
Author

Benjamin M commented

Thanks for the quick response. And what about the Query no limiting/sorting?

BTW: Is it possible to create a normal MongoRepository to access the GridFS contents? (just id, filename, etc. without the file blob itself)

@spring-projects-issues
Copy link
Author

Benjamin M commented

I've answered my question from above myself. So maybe you could just provide some GridFSDocument and some GridFSRepository. I've created (only for testing):

@Document(collection="fs.files")
public class GridFSDocument {
@Id
public ObjectId id;
@Field
public String filename;
@Field
public Date uploadDate;
}

public interface GridFSRepository extends MongoRepository<GridFSDocument, ObjectId>{}

Now I can query everything I want to, including Pageable, Sort, limit().... Where are the negative aspects?
The only thing I can't do, is getting the InputStream, but that's not necessary for my current use case

@spring-projects-issues
Copy link
Author

Oliver Moser commented

what works for me is simply using a mongoTemplate instance instead:

List<GridFSDBFile> gridFsFiles = mongoTemplate.find(query, GridFSDBFile.class, "fs.files");

which also gives me access to the inputStream of the stored file

@spring-projects-issues
Copy link
Author

Oliver Moser commented

\
I realized that using mongoTemplate instead is not enough since there is no associated GridFS instance in this case. So, what you can do is wrap the GridFSDBFile you're working with in something like the following:
\

package com.mongodb.gridfs;

public class GridFSDBFileWrapper extends GridFSDBFile {

    public GridFSDBFileWrapper (GridFSDBFile file, GridFS fs) {
        this._aliases = file._aliases;
        this._chunkSize = file._chunkSize;
        this._contentType = file._contentType;
        this._extradata = file._extradata;
        this._filename = file._filename;
        this._fs = fs;
        this._id = file._id;
        this._length = file._length;
        this._md5 = file._md5;
        this._uploadDate = file._uploadDate;
    }
}

you can autowire the mongoDbFactory instance in your service class and then wrap the instances returned by the mongoTemplate like this
\

private GridFSDBFile wrapGridFSDBFile(GridFSDBFile file) {
        DB db = mongoDbFactory.getDb();
        return new GridFSDBFileWrapper(file, new GridFS(db));
}

hope it helps

@spring-projects-issues
Copy link
Author

Mark Paluch commented

Since we switched to Document API we can benefit from GridFSFindIterable.sort/skip/collation and introduce a proper API to query GridFSFile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants