2015-03-02 28 views
2

我有一個文件在MongoDB中如下:乘3的值,從提取的MongoDB在java中

"sentiment" : { 
    "Value" : 0, 
    "high" : 0 
}, 
"quality" : { 
    "Value" : 0, 
    "high" : 0 
}, 
"intensity" : { 
    "Value" : 0, 
    "low" : 0 
} 

現在我想乘quality.value intensity.value sentiment.value然後在這裏返回結果是我的代碼:

DBObject groupFields = new BasicDBObject("_id", "$Added"); 
    DBObject project = null; 
     groupFields.put("value1", new BasicDBObject("$sum", 
       "$sentiment.Value")); 
     groupFields.put("value2", new BasicDBObject("$sum", 
       "$quality.Value")); 
     groupFields.put("value3", new BasicDBObject("$sum", 
       "$intensity.Value")); 
     groupFields.put("count", new BasicDBObject("$sum", 1)); 
     DBObject groupBy = new BasicDBObject("$group", groupFields); 
     stages.add(groupBy); 
     DBObject valueMultiply = new BasicDBObject("$multiply", args); 
     project = new BasicDBObject("_id", 0); 
     project.put("value", valueMultiply); 
     AggregationOutput output = collectionG.aggregate(stages); 

現在我的問題是這樣的一行:

DBObject valueMultiply = new BasicDBObject("$multiply", args); 

我不知道如何做他的乘法value1和value2和value3 ...

任何人都可以幫忙嗎?

回答

2

您需要構建您的輸入$multiply操作員的Objects作爲陣列中,

args = new Object[]{"$value1","$value2","$value3"}; 

,並用它作爲:

DBObject valueMultiply = new BasicDBObject("$multiply", args); 

$multiply運營商採用的expressions陣列,有效表達式包括字段路徑並保持有效直到字段路徑解析爲數值。在這種情況下,字段路徑$value1$value2$value3解析爲數字,因此是有效的表達式。

+1

真棒回答@BatScream ...感謝萬萬:) – 2015-03-03 15:13:12