代码之家  ›  专栏  ›  技术社区  ›  user405398

查询MongoDB中的数组位置?

  •  3
  • user405398  · 技术社区  · 15 年前

    我正在使用MavigDB的Java驱动程序。我有一份文件, AlbumName , PreviewSize , ThumbSize , _id 和; Comments . 预览尺寸 和; 拇指尺寸 是数组。我想知道如何查询这个文档并获取任何特定的数组值。

    更清楚地说,我的文件是这样的,

    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> 预览尺寸 .

    我是MongoDB的新手。我不知道是否可能。我刚刚构建了这个文档,我可以用新的图像更新这个文档。我从中学到这个 this 演示。

    我的问题是我怎样才能得到任何具体的 <二进制数据 当我知道它的名字?

    任何建议都会更有价值!

    谢谢!!!!

    更新

    例如:/,我尝试的代码,

    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??

    1 回复  |  直到 15 年前
        1
  •  4
  •   Gates VP    15 年前

    db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"} )

    db.albums.find({ "ThumbSize.ImageName" : "TomJerry2_468x342.jpg"}, { "ThumbSize" : 1 } )