2016-01-26 70 views
4

我在蒙戈以下文件:獲得價值從嵌入文檔中蒙戈的Java

> { "_id": ObjectId("569afce4b932c542500143ec"),  
> "date": "2016-1-17T2:31:0Z",  
> "day": NumberInt(17),  
> "model1": { 
>  "date": "2016-01-17T02:31+0000", 
>  "MondayModel": { 
>  "gtxdotdot": { 
>   "xdotdot": 0, 
>   "xdot": 0 
>  }, 
>  "lsxdotdot": { 
>   "xdotdot": 0, 
>   "xdot": 0 
>  }, 
>  "gtxdot": { 
>   "xdotdot": 0, 
>   "xdot": 0 
>  }, 
>  "lsxdot": { 
>   "xdotdot": 0, 
>   "xdot": 0 
>  }, 
>  "modeldotdot": { 
>   "mean": 0, 
>   "sdvar": 0 
>  }, 
>  "modeldot": { 
>   "mean": 0, 
>   "sdvar": 0 
>  } 
>  } 
>  } 

我希望雙方找到這個文件並提取model1.MondayModel.gtxdotdot.xdotdot/xdot/mean/sdvar只值...

我當前的代碼呢所以有以下幾點:

MongoCursor<Document> back = collection.find(and(eq("topic",topic),eq("sp",sp))).limit(1).iterator(); 
if (back.hasNext()) 
{ 
    Document doc = back.next(); 
    Document tmpddc1 = (Document)doc.get("model1"); 
    Document tmpddc2 = (Document)tmpddc1.get("MondayModel"); 

    Document tmpddc3 = (Document)tmpddc2.get("gtxdotdot"); 
    gtxdotdotXdotdot = tmpddc3.getDouble("xdotdot"); 
    gtxdotdotXdot = tmpddc3.getDouble("xdot"); 

      tmpddc3 = (Document)tmpddc2.get("lsxdotdot"); 
    lsxdotdotXdotdot = tmpddc3.getDouble("xdotdot"); 
    lsxdotdotXdot = tmpddc3.getDouble("xdot"); 

      tmpddc3 = (Document)tmpddc2.get("gtxdot"); 
    gtxdotXdotdot = tmpddc3.getDouble("xdotdot"); 
    gtxdotXdot = tmpddc3.getDouble("xdot");    

      tmpddc3 = (Document)tmpddc2.get("lsxdot"); 
    lsxdotXdotdot = tmpddc3.getDouble("xdotdot"); 
    lsxdotXdot = tmpddc3.getDouble("xdot"); 


      tmpddc3 = (Document)tmpddc2.get("modeldotdot"); 
    modeldotdotXmean = tmpddc3.getDouble("mean"); 
    modeldotdotXsdvar = tmpddc3.getDouble("sdvar");    

      tmpddc3 = (Document)tmpddc2.get("modeldot"); 
    modeldotXmean = tmpddc3.getDouble("mean"); 
    modeldotXsdvar = tmpddc3.getDouble("sdvar");       

} 

,而不是運行的認爲他的文件(如上)有沒有辦法讓這些值使用點符號[model1.MondayModel.gtxdotdot.xdotdot] ?例如:

double value = doc.getDouble("model1.MondayModel.gtxdotdot.xdotdot"); 

回答

0

我不認爲你可以直接使用點符號,但你可以創建自己的幫助函數。

解決方案1:獲得場點符號

public static Object getWithDotNotation(Document document, String dots) 
    throws MongoException{ 

    String[] keys = dots.split("\\."); 
    Document doc = document; 

    for(int i = 0; i < keys.length - 1; i++){ 
     Object o = doc.get(keys[ i ]); 
     if(o == null || !(o instanceof Document)){ 
      throw new MongoException(String.format( 
        "Field '%s' does not exist or s not a Document", keys[ i ])); 
     } 
     doc = (Document) o; 
    }//end for 

    return doc.get(keys[ keys.length - 1 ]); 
} 

然後,您可以使用它像這樣:

String dotNotation = "model1.MondayModel.gtxdotdot.xdotdot"; 

FindIterable<Document> projection = mongoColl.find() 
    .projection(fields(include(dotNotation))); 

Double value = (Double) getWithDotNotation(projection.first(), dotNotation); 

System.out.println(value); // result: 0.0 

這將簡化代碼一大堆。照顧唯一的事情是:

  • ,如果你不知道你點記號的,使用try catch
  • 方法getWithDotNotation可能返回null
  • 小心強制類型轉換,只有在使用它你100%確信你的數據類型(這裏是Double)。

解決方案2:扁平化您的文件

public static Document flattenDoc(Document document){ 

    Document flattened = new Document(); 
    Queue<Pair<String, Document>> queue = new ArrayDeque<>(); 
    queue.add(new Pair<>("", document)); 

    while(!queue.isEmpty()){ 
     Pair<String, Document> pair = queue.poll(); 
     String key = pair.getKey(); 
     for(Map.Entry<String, Object> entry : pair.getValue().entrySet()){ 
      if(entry.getValue() instanceof Document){ 
       queue.add(new Pair<>(key + entry.getKey() + ".", (Document) entry.getValue())); 

      }else{ 
       flattened.put(key + entry.getKey(), entry.getValue()); 

      } 
     }//end for 
    } 

    return flattened; 
} 

與樣品的數據,flattenDoc結果如下:

Document{{_id=569afce4b932c542500143ec, 
date=2016-1-17T2:31:0Z, 
day=17, 
model1.date=2016-01-17T02:31+0000, 
model1.MondayModel.gtxdotdot.xdotdot=0.0, 
model1.MondayModel.gtxdotdot.xdot=0.0, 
model1.MondayModel.lsxdotdot.xdotdot=0.0, 
model1.MondayModel.lsxdotdot.xdot=0.0, 
model1.MondayModel.gtxdot.xdotdot=0.0, 
model1.MondayModel.gtxdot.xdot=0.0, 
model1.MondayModel.lsxdot.xdotdot=0.0, 
model1.MondayModel.lsxdot.xdot=0.0, 
model1.MondayModel.modeldotdot.mean=0.0, 
model1.MondayModel.modeldotdot.sdvar=0.0, 
model1.MondayModel.modeldot.mean=0.0, 
model1.MondayModel.modeldot.sdvar=0.0}} 

這樣你就可以直接使用getDouble("model1.MondayModel.gtxdotdot.xdotdot")。如果您需要訪問所有字段,此方法可能會更有效。