簡體   English   中英

如何使用MongoDB Java驅動程序對ISODate屬性的dayOfYear進行分組?

[英]How to use MongoDB Java driver to group by dayOfYear on ISODate attributes?

如何使用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());
}

根據此評論進行更新

項目的問題之一是它僅包含您指定的字段。 以上輸入僅是示例。 我的文檔中有100個字段,我無法將每個字段都分隔開,因此,如果我使用項目,則除了“ dayOfYear”字段外,還必須指定所有100個字段。 – user1870400 11分鍾前

您可以使用以下命令返回相同的輸出,但沒有$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());
}

注意:根據集合的大小/您的非功能性要求/等,您可能要考慮這些解決方案的性能,並且(a)在開始投影/編輯之前添加匹配階段,或者(b)將dayOfYear提取為自己的屬性,這樣您就可以完全避免這種復雜性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM