java - 如何使用 MongoDB Java 驱动程序按 ISODate 属性上的 dayOfYear 进行分组?

标签 java mongodb mongodb-query aggregation-framework mongodb-java

如何使用 mongodb java 驱动程序比较两个 ISODate 对象的 dayOfYear?

这是我的文档

{"name": "hello", "count": 4, "TIMESTAMP": ISODate("2017-10-02T02:00:35.098Z")}
{"name": "hello", "count": 5, "TIMESTAMP": ISODate("2017-10-02T02:00:35.098Z")}
{"name": "goodbye", "count": 6, "TIMESTAMP": ISODate("2017-10-01T02:00:35.098Z")}
{"name": "foo", "count": 6, "TIMESTAMP": ISODate("2017-10-02T02:00:35.098Z")}

我想比较“TIMESTAMP”中的日期来执行一些聚合

 Bson match = Aggregates.match(eq("name": "hello"));
 Bson group = Aggregates.group(new Document("name", "$name"), Accumulators.sum("total", 1));

collection.aggregate(Arrays.asList(match, group))

现在我不确定如何对属于特定日期的所有记录进行此聚合?

所以我对“2017-10-02”的预期结果是

[{"_id": {"name":"hello"}, "total": 9}, {"_id": {"name":"foo"}, "total": 6} ]

最佳答案

鉴于以下文件:

{"name": "hello", "count": 4, "TIMESTAMP": ISODate("2017-10-02T02:00:35.098Z")}
{"name": "hello", "count": 5, "TIMESTAMP": ISODate("2017-10-02T02:00:35.098Z")}
{"name": "goodbye", "count": 6, "TIMESTAMP": ISODate("2017-10-01T02:00:35.098Z")}
{"name": "foo", "count": 6, "TIMESTAMP": ISODate("2017-10-02T02:00:35.098Z")}

以下命令...

db.getCollection('dayOfYear').aggregate([

    // project dayOfYear as an attribute
    { $project: { name: 1, count: 1, dayOfYear: { $dayOfYear: "$TIMESTAMP" } } },

    // match documents with dayOfYear=275
    { $match: { dayOfYear: 275 } },

    // sum the count attribute for the selected day and name
    { $group : { _id : { name: "$name" }, total: { $sum: "$count" } } } 

])

...将返回:

{
    "_id" : {
        "name" : "foo"
    },
    "total" : 6
}

{
    "_id" : {
        "name" : "hello"
    },
    "total" : 9
}

认为这满足您OP中表达的要求。

这是使用 MongoDB Java 驱动程序表达的相同命令:

MongoCollection<Document> collection = mongoClient.getDatabase("stackoverflow").getCollection("dayOfYear");

Document project = new Document("name", 1)
        .append("count", 1)
        .append("dayOfYear", new Document("$dayOfYear", "$TIMESTAMP"));

Document dayOfYearMatch = new Document("dayOfYear", 275);

Document grouping = new Document("_id", "$name").append("total", new Document("$sum", "$count"));

AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
        new Document("$project", project),
        new Document("$match", dayOfYearMatch),
        new Document("$group", grouping)
));

for (Document document : documents) {
    logger.info("{}", document.toJson());
}

根据此评论更新:

One of the problems with project is that it only include fields you specify . The above input is just an example. I have 100 fields in my doc I can't sepecify every single one so if I use project I have to specify all 100 fields in addition to "dayOfYear" field. – user1870400 11 mins ago

您可以使用以下命令返回相同的输出,但没有 $project 阶段:

db.getCollection('dayOfYear').aggregate([
    // ignore any documents which do not match dayOfYear=275
    { "$redact": {
        "$cond": {
             if: { $eq: [ { $dayOfYear: "$TIMESTAMP" }, 275 ] },
             "then": "$$KEEP",
             "else": "$$PRUNE"
        }
    }},

    // sum the count attribute for the selected day
    { $group : { _id : { name: "$name" }, total: { $sum: "$count" } } } 

])

这是该命令的“Java 形式”:

MongoCollection<Document> collection = mongoClient.getDatabase("stackoverflow").getCollection("dayOfYear");

Document redact = new Document("$cond", new Document("if", new Document("$eq", Arrays.asList(new Document("$dayOfYear", "$TIMESTAMP"), 275)))
        .append("then", "$$KEEP")
        .append("else", "$$PRUNE"));

Document grouping = new Document("_id", "$name").append("total", new Document("$sum", "$count"));

AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
        new Document("$redact", redact),
        new Document("$group", grouping)
));

for (Document document : documents) {
    logger.info("{}", document.toJson());
}

注意:根据您的 Collection 的大小/您的非功能性需求/等,您可能需要考虑这些解决方案的性能,并且 (a) 在开始投影之前添加匹配阶段/编辑或 (b) 将 dayOfYear 提取到其自己的属性中,以便您可以完全避免这种复杂性。

关于java - 如何使用 MongoDB Java 驱动程序按 ISODate 属性上的 dayOfYear 进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46536597/

相关文章:

javascript - 使 meteor 出版物对时间有反应

mongodb - 从文档中删除空字段的名称

java - 打印 .2f 问题

Java POI编辑excel文件-cell.setCellValue不起作用

c# - 如何使用具有 native JSON 标准语法的 MongoDB C# 驱动程序发出查找命令?

javascript - 如何将 Mongoose/Mongodb 与 node.js- Passport 身份验证一起使用

java - 如何使用数组列表存储坐标以在固定区域内绘制不同颜色的随机点?

java - 在 Android Studio 上设置 L

node.js - 查询数组时 MongoDB 数据匹配和获取问题

mongodb - 在 MongoDB 中创建数据库并从文件中插入数据