我在MongoDB中有一个文档,如下所示:
"sentiment" : {
"Value" : 0,
"high" : 0
},
"quality" : {
"Value" : 0,
"high" : 0
},
"intensity" : {
"Value" : 0,
"low" : 0
}
现在我想乘以quality.valueintensity.valuemomentation.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);
我不知道怎么把1,2,3相乘…
有人能帮忙吗?
最佳答案
您需要将$multiply
运算符的输入构造为Objects
数组,如中所示,
args = new Object[]{"$value1","$value2","$value3"};
并将其用作:
DBObject valueMultiply = new BasicDBObject("$multiply", args);
$multiply
运算符采用expressions数组,有效表达式包括字段路径,并在字段路径解析为数值之前保持有效。在这种情况下,字段路径、$value1
、$value2
和$value3
解析为数字,因此是有效的表达式。关于java - 在java中从mongodb中提取的3值的乘法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28803153/