What I'm trying to do is fetching a list of GridFS files by querying an field of the metadata. For example I got a GridFS file document looking like:
{ "_id" : { "$oid" : "4f95475f5ef4fb269dbac954"} , "chunkSize" : 262144 , "length" : 3077 , "md5" : "f24ea7ac05c5032f08808c6faabf413b" , "filename" : "file_xyz.txt" , "contentType" : null , "uploadDate" : { "$date" : "2012-04-23T12:13:19.606Z"} , "aliases" : null , "metadata" : { "target_field" : "abcdefg"}}
And I want to query all files containing "target_field" = "abcdefg". I created my query as follows:
BasicDBObject query = new BasicDBObject("metadata", new BasicDBObject("target_field", "abcdefg"));
// gridFS Object Initialization skipped
List<GridFSDBFile> files = gridFs.find(query);
The list is allways empty. Otherwise querying the filename or uploadDate works perfectly. Isn't it possible to get the GridFS files by nested attributes?
Unfortunately I didn't get it to work with nested BasicDBObjects.
Finally I was using the dot notation which works fine:
// This query fetches the files I need
BasicDBObject query = new BasicDBObject("metadata.target_field", "abcdefg"));
List<GridFSDBFile> files = gridFs.find(query);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With