我正在为mongoDB使用Java Driver。我的文档包含AlbumName
,PreviewSize
,ThumbSize
,_id
& Comments
。 PreviewSize
& ThumbSize
是数组。我想知道如何查询此文档并获取任何特定的数组值。
为了更清楚,我的文档是这样的,
Document : { "AlbumName" : "Test" , "PreviewSize" : [
{ "ImageName" : "tom_and_jerry_guns.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "Tom_and_Jerry,_Cartoons.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "TomJerry2_468x342.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "tom_and_jerry-5405.jpg" , "BinaryImage" : <Binary Data>}] ,
"ThumbSize" : [ { "ImageName" : "tom_and_jerry_guns.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "Tom_and_Jerry,_Cartoons.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "TomJerry2_468x342.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "tom_and_jerry-5405.jpg" , "BinaryImage" : <Binary Data>}] ,
"_id" : { "$oid" : "4cef03b44613b5ba2d51a7b8"} , "comments" : [ ]}
如何从TomJerry2_468x342.jpg
获取<Binary Data>
及其PreviewSize
。
我是mongoDB的新手。我不知道它是否可能。我刚刚构建了这个文档,我可以用新图像更新这个文档。我是从this演示文稿中学到的。
我的问题如果我有名字,我怎么能得到任何具体的<Binary Data>
?
任何建议都会更有意义!
感谢!!!
更新
例如://我试过的代码,
BasicDBObject query = new BasicDBObject();
BasicDBObject field = new BasicDBObject();
field.put("PreviewSize", 1);
DBCursor cursor = coll.find(query, field);
while(cursor.hasNext()){
BasicDBObject result = (BasicDBObject) cursor.next();
int i = result.size();
System.out.println("Result Size: "+i);
System.out.println(result);
int i = result.size();
String imageName = (String) ((BasicDBList)result.get("PreviewSize")).get("ImageName");
System.out.println("Result Size: "+i+" Image Name :"+imageName);
byte[] imageByte = (byte[]) ((BasicDBList) result.get("PreviewSize")).get("BinaryImage");
if(imageByte != null){System.out.println("Wow! Great!!! Its Coming!!!");}
else{System.out.println("Try Another Way!");}
}// I am not sure this is exist(I am new to mongoDB). I need
something like that.
/* Some thing like the above list contains All contents of the
"PreviewSize" data <ImageName> & <BinaryData>.*/
首先我尝试通过(BasicDBObject)进行投射,但是我给了我一个错误,
cannot cast (BasicBSONList) to (BasicDBObject)
然后我将其更改为(BasicBSONList)
我认为我在接收器中做错了。我应该使用什么接收器!!! ???
我尝试了上面的那个,我在控制台中收到的错误信息是,
控制台显示:
Result Size: 2
{ "PreviewSize" : [ { "ImageName" : "tom_and_jerry_guns.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "Tom_and_Jerry,_Cartoons.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "TomJerry2_468x342.jpg" , "BinaryImage" : <Binary Data>} ,
{ "ImageName" : "tom_and_jerry-5405.jpg" , "BinaryImage" : <Binary Data>}] ,
"_id" : { "$oid" : "4cef03b44613b5ba2d51a7b8"}}
"Exception in thread "main" java.lang.IllegalArgumentException:
BasicBSONList can only work with numeric keys, not: [ImageName]"
我是mongoDB的新手,就像3天大一样:( ....给我一个方法 出来!!!!
而且,我也想知道有没有办法检索特定数据,
就像,如果我知道任何ImageName
我可以得到BinaryData??
答案 0 :(得分:4)
查询应该是:
db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"} )
这将返回包含所有二进制数据的Album文档。
你将无法返回那一件。但是,您可以使用以下命令返回“Thumbsize数组”。
db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"}, { "ThumbSize" : 1 } )
但是,如果您有几个大图像,您将需要查看GridFS以存储这些图像,然后制作一个。