MongoDB 分组操作 return 文档而不是单个字段
MongoDB Group operation return document rather than single fields
我正在尝试根据我在 Mongo 中的数据,根据上次访问文件的时间对文件集合进行分组。
但是我不确定如何从组操作中 return 更多文档。
如何取回包含每个分组下所有信息的文档?
例如我现在 returning:
[
{
"year": [
2020
],
"month": [
2
],
"week": [
7
],
"day": [
2
],
"results": [
"filename-1",
"filename-2"
]
}
]
我只是在基本数组中取回文件名。我想要文档而不仅仅是名称。
我用来创建上面结果的代码
public void groupTest() {
final String dateField = "lastAccessTime";
final LocalDate now = LocalDate.now();
final LocalDate firstDay = now.with(firstDayOfYear());
final LocalDate lastDay = now.with(lastDayOfYear());
final Criteria criteria =
new Criteria().andOperator(where(dateField).gte(firstDay), where(dateField).lte(lastDay));
final ProjectionOperation dateProjection =
project()
.and("_id")
.as("id")
.and("name")
.as("name")
.and("absolutePath")
.as("absolutePath")
.and(dateField)
.extractYear()
.as("year")
.and(dateField)
.extractMonth()
.as("month")
.and(dateField)
.extractWeek()
.as("week")
.and(dateField)
.extractDayOfWeek()
.as("day");
final GroupOperation groupBy =
group("year", "month", "week", "day")
.addToSet("name")
.as("results");
final Aggregation aggregation =
newAggregation(
match(criteria),
dateProjection,
groupBy,
sort(Sort.Direction.ASC, "year", "month", "week", "day"));
}
我希望我的结果值更像:
[
{
"year": [
2020
],
"month": [
2
],
"week": [
7
],
"day": [
2
],
"results": [
{
"id": "123",
"filename": "filename-1",
"size": 30000
},
{
"id": "456",
"filename": "filename-2",
"size": 30000
}
]
}
]
非常感谢任何帮助
将你的小组赛阶段改为:
GroupOperation group = Aggregation
.group("year", "month", "week", "day")
.addToSet(new Document("id", "$id")
.append("filename", "$name")
.append("size", "$size"))
.as("results");
注意: 确保将 size
字段包含到 dateProjection
阶段。
我正在尝试根据我在 Mongo 中的数据,根据上次访问文件的时间对文件集合进行分组。
但是我不确定如何从组操作中 return 更多文档。
如何取回包含每个分组下所有信息的文档?
例如我现在 returning:
[
{
"year": [
2020
],
"month": [
2
],
"week": [
7
],
"day": [
2
],
"results": [
"filename-1",
"filename-2"
]
}
]
我只是在基本数组中取回文件名。我想要文档而不仅仅是名称。
我用来创建上面结果的代码
public void groupTest() {
final String dateField = "lastAccessTime";
final LocalDate now = LocalDate.now();
final LocalDate firstDay = now.with(firstDayOfYear());
final LocalDate lastDay = now.with(lastDayOfYear());
final Criteria criteria =
new Criteria().andOperator(where(dateField).gte(firstDay), where(dateField).lte(lastDay));
final ProjectionOperation dateProjection =
project()
.and("_id")
.as("id")
.and("name")
.as("name")
.and("absolutePath")
.as("absolutePath")
.and(dateField)
.extractYear()
.as("year")
.and(dateField)
.extractMonth()
.as("month")
.and(dateField)
.extractWeek()
.as("week")
.and(dateField)
.extractDayOfWeek()
.as("day");
final GroupOperation groupBy =
group("year", "month", "week", "day")
.addToSet("name")
.as("results");
final Aggregation aggregation =
newAggregation(
match(criteria),
dateProjection,
groupBy,
sort(Sort.Direction.ASC, "year", "month", "week", "day"));
}
我希望我的结果值更像:
[
{
"year": [
2020
],
"month": [
2
],
"week": [
7
],
"day": [
2
],
"results": [
{
"id": "123",
"filename": "filename-1",
"size": 30000
},
{
"id": "456",
"filename": "filename-2",
"size": 30000
}
]
}
]
非常感谢任何帮助
将你的小组赛阶段改为:
GroupOperation group = Aggregation
.group("year", "month", "week", "day")
.addToSet(new Document("id", "$id")
.append("filename", "$name")
.append("size", "$size"))
.as("results");
注意: 确保将 size
字段包含到 dateProjection
阶段。