问题描述
我想做的是通过查询元数据的字段来获取GridFS文件列表.例如,我得到了一个如下所示的GridFS文件文档:
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"}}
我想查询所有包含"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);
列表始终为空.否则,查询文件名或uploadDate的效果很好.是否可以通过嵌套属性获取GridFS文件?
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?
推荐答案
不幸的是,我没有让它与嵌套的BasicDBObjects一起使用.
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);
这篇关于查询MongoDB GridFS元数据(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!