gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:34:52 25 4
gpt4 key购买 nike

如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com