MongoDB聚合减去Java驱动程序3.3
我在下面的shell查询中使用了减法操作符,但是我不知道如何在Java中进行减法
db.deviceDetail.aggregate([
{$match: {'accountId':23, 'status':'Stopped'}},
{'$project': {
'durationDiff': {'$subtract': [1479093620000, '$durationDate']},
'stopStartDiff': {'$subtract': [1479093620000, '$stopStart']},
'stopStart': 1,
'durationDate': 1,
'_id':0
}
},
{'$match': {$or:[{'durationDiff': {'$gt': 0}}, {'stopStartDiff': {'$gt': 0}}]}}
])
Java API似乎不支持减法运算符,那么下面应该如何定义durationDiff和stopStartDiff计算
AggregateIterable<Document> rslt = coll.aggregate(Arrays.asList(
match(and(eq("assetName", "accountId"), eq("assetName", "accountId"))),
project(fields(
excludeId(),
include(
"durationDiff", ...,
"stopStartDiff", ...,
"stopStart",
"durationDate"
)
)),
match(or(gt("durationDiff", 0), gt("stopStartDiff", 0)))
));
样本文件:
{
"_id" : ObjectId("563064a4e4b0ea032ae14f77"),
"accountId" : NumberLong(23),
"stopStart" : NumberLong("1460133175000"),
"status" : "Stopped",
"durationDate" : NumberLong("1460133175000")
}
# 1 楼答案
你可以尝试一些类似的方法来解决你所遇到的简单用例。对于其他变量,也必须执行同样的操作。这将计算durationDiff
两个人。x版本
三个人。x版本
使用BsonDocument-类型安全版本
使用文档-非类型安全版本