Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query on MongoDB GridFS metadata (Java)

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?

like image 340
sebastian Avatar asked Apr 23 '12 13:04

sebastian


1 Answers

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);
like image 145
sebastian Avatar answered Oct 18 '22 15:10

sebastian