Java 为什么';我的Mongo聚合不能在嵌套文档上正常工作吗?
我有以下课程:Java 为什么';我的Mongo聚合不能在嵌套文档上正常工作吗?,java,spring,mongodb,aggregation-framework,mongotemplate,Java,Spring,Mongodb,Aggregation Framework,Mongotemplate,我有以下课程: @Document(collection = "rCollection") public class R{ public R() { } @Id private String id; @TextIndexed(weight=2) private String fp; @TextIndexed(weight=1) private String sp; @DBRef @Indexed pri
@Document(collection = "rCollection")
public class R{
public R() {
}
@Id
private String id;
@TextIndexed(weight=2)
private String fp;
@TextIndexed(weight=1)
private String sp;
@DBRef
@Indexed
private P p;
@DBRef
private User user;
private String date;
}
以及:
我想做的是根据类的p
元素在RCollection
和group
中搜索结果。由于P
类中的id
是唯一的,因此我尝试根据P.id
对结果进行分组。
我的存储库中有以下代码:
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
MatchOperation match = Aggregation.match(criteria);
GroupOperation group = Aggregation.group("p.id");
Aggregation aggregation = Aggregation.newAggregation(match, group);
AggregationResults<finalResults> results = mongoTemplate.aggregate(aggregation, "rCollection", finalResults.class);
List<finalResults> result= results.getMappedResults();
System.out.println("size is: "+result.size());
System.out.println("output = "+result.get(0).toString());
这是我的finalResults
类(它实际上与R
类完全相同):
更新:
样本数据:
我在r集合中拥有的一切
:
{
"_id" : ObjectId("5e9db54c3b4b97129065b773"),
"_class" : "com.main.tothepoint.model.R",
"fp" : "hello\ni\nam\n",
"sp " : "hi",
"date" : "Mon Apr 20 16:44:28 CEST 2020",
"p" : DBRef("p", ObjectId("5e9db54c3b4b97129065b772")),
"user" : DBRef("user", ObjectId("5e5aa321383ba54434092e8d"))
}
{
"_id" : ObjectId("5e9db7903b4b97129065b775"),
"_class" : "com.main.tothepoint.model.R",
"fp " : "hi",
"sp " : "hello",
"date" : "Mon Apr 20 16:54:08 CEST 2020",
"p" : DBRef("p", ObjectId("5e9db7903b4b97129065b774")),
"user" : DBRef("user", ObjectId("5e6e567dc77ed32ab4ad796b"))
}
{
"_id" : ObjectId("5e9db7a73b4b97129065b777"),
"_class" : "com.main.tothepoint.model.R",
"fp" : "hi",
"sp" : "",
"date" : "Mon Apr 20 16:54:31 CEST 2020",
"p" : DBRef("p", ObjectId("5e9db7a73b4b97129065b776")),
"user" : DBRef("user", ObjectId("5e6e567dc77ed32ab4ad796b"))
}
{
"_id" : ObjectId("5e9ef7e04b6e5d338c02fda0"),
"_class" : "com.main.tothepoint.model.R",
"fp" : "hello",
"sp" : "",
"date" : "Tue Apr 21 15:40:48 CEST 2020",
"p" : DBRef("p", ObjectId("5e9ef7e04b6e5d338c02fd9f")),
"user" : DBRef("user", ObjectId("5e5aa321383ba54434092e8d"))
}
{
"_id" : ObjectId("5e9ef82b4b6e5d338c02fda3"),
"_class" : "com.main.tothepoint.model.R",
"fp " : "hello",
"sp" : "",
"date" : "Tue Apr 21 15:42:03 CEST 2020",
"p" : DBRef("p", ObjectId("5e9ef82b4b6e5d338c02fda2")),
"user" : DBRef("user", ObjectId("5e9ef81f4b6e5d338c02fda1"))
}
{
"_id" : ObjectId("5e9db54c3b4b97129065b772"),
"_class" : "com.main.tothepoint.model.P",
"p_name" : "Hisar",
"longitude" : 75.7216527,
"latitude" : 29.1491875
}
{
"_id" : ObjectId("5e9db7a73b4b97129065b776"),
"_class" : "com.main.tothepoint.model.P",
"p_name" : "Helsinki",
"longitude" : 24.9383791,
"latitude" : 60.16985569999999
}
以及pCollection
中的相关部分:
{
"_id" : ObjectId("5e9db54c3b4b97129065b773"),
"_class" : "com.main.tothepoint.model.R",
"fp" : "hello\ni\nam\n",
"sp " : "hi",
"date" : "Mon Apr 20 16:44:28 CEST 2020",
"p" : DBRef("p", ObjectId("5e9db54c3b4b97129065b772")),
"user" : DBRef("user", ObjectId("5e5aa321383ba54434092e8d"))
}
{
"_id" : ObjectId("5e9db7903b4b97129065b775"),
"_class" : "com.main.tothepoint.model.R",
"fp " : "hi",
"sp " : "hello",
"date" : "Mon Apr 20 16:54:08 CEST 2020",
"p" : DBRef("p", ObjectId("5e9db7903b4b97129065b774")),
"user" : DBRef("user", ObjectId("5e6e567dc77ed32ab4ad796b"))
}
{
"_id" : ObjectId("5e9db7a73b4b97129065b777"),
"_class" : "com.main.tothepoint.model.R",
"fp" : "hi",
"sp" : "",
"date" : "Mon Apr 20 16:54:31 CEST 2020",
"p" : DBRef("p", ObjectId("5e9db7a73b4b97129065b776")),
"user" : DBRef("user", ObjectId("5e6e567dc77ed32ab4ad796b"))
}
{
"_id" : ObjectId("5e9ef7e04b6e5d338c02fda0"),
"_class" : "com.main.tothepoint.model.R",
"fp" : "hello",
"sp" : "",
"date" : "Tue Apr 21 15:40:48 CEST 2020",
"p" : DBRef("p", ObjectId("5e9ef7e04b6e5d338c02fd9f")),
"user" : DBRef("user", ObjectId("5e5aa321383ba54434092e8d"))
}
{
"_id" : ObjectId("5e9ef82b4b6e5d338c02fda3"),
"_class" : "com.main.tothepoint.model.R",
"fp " : "hello",
"sp" : "",
"date" : "Tue Apr 21 15:42:03 CEST 2020",
"p" : DBRef("p", ObjectId("5e9ef82b4b6e5d338c02fda2")),
"user" : DBRef("user", ObjectId("5e9ef81f4b6e5d338c02fda1"))
}
{
"_id" : ObjectId("5e9db54c3b4b97129065b772"),
"_class" : "com.main.tothepoint.model.P",
"p_name" : "Hisar",
"longitude" : 75.7216527,
"latitude" : 29.1491875
}
{
"_id" : ObjectId("5e9db7a73b4b97129065b776"),
"_class" : "com.main.tothepoint.model.P",
"p_name" : "Helsinki",
"longitude" : 24.9383791,
"latitude" : 60.16985569999999
}
数据比我问题的第一部分包含更多的元素,因为我曾试图使示例类尽可能小。
在我的代码中,我想搜索可以存在于fp
和sp
中的文本,例如,我搜索“hi”,然后我看到结果的大小是2187。问题在于:
GroupOperation group = Aggregation.group("p.id");
在MongoDB shell中,情况如下:
{$group:{ _id:"$p._id" }}
如你所见,你的小组阶段还没有完成。您需要在分组阶段累积fp
和sp
字段。要计算fp
和sp
字段的所有可能值,我们需要使用$push
(具有重复值的数组)或$addToSet
(使用唯一值设置)操作符并添加额外的$unwind
。舞台
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
MatchOperation match = Aggregation.match(criteria);
GroupOperation group = Aggregation.group("p.id").push("fp").as("fp").push("sp").as("sp");
UnwindOperation unwindFp = Aggregation.unwind("fp");
UnwindOperation unwindSp = Aggregation.unwind("sp");
Aggregation aggregation = Aggregation.newAggregation(match, group, unwindFp, unwindSp);
AggregationResults<finalResults> results = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(R.class), FinalResults.class);
TextCriteria=TextCriteria.forDefaultLanguage().matchingAny(文本);
MatchOperation match=聚合。匹配(标准);
GroupOperation group=Aggregation.group(“p.id”).push(“fp”).as(“fp”).push(“sp”).as(“sp”);
解算解算fp=聚合解算(“fp”);
解算解算sp=聚合解算(“sp”);
聚合聚合=聚合.newAggregation(匹配、组、unwindFp、unwindSp);
AggregationResults=mongoTemplate.aggregate(聚合,mongoTemplate.getCollectionName(R.class),FinalResults.class);
非常感谢您的回答。fp和sp可以不同。对不起,我对spring boot和mongodb还不太熟悉,我知道.push(“fp”).as(“fp”),但我不知道如何实现push和addToSet的组合。(或者我理解错你的答案了吗?)你能再解释一下吗?谢谢你advance@user1419243更新了我的答案,请再次检查。感谢您的更新,您可以重新生成查询。然而,它仍然不能正确地工作。我不再获取空项,但现在我的输出大小为2187,尽管我的数据库中只有5个项,其中3个案例有搜索字符串,其中2个共享同一个place.id。因此,我希望得到两个包含所有3项的组。@user1419243 post please sample data我更新了问题。再次感谢您的关注。
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
MatchOperation match = Aggregation.match(criteria);
GroupOperation group = Aggregation.group("p.id").push("fp").as("fp").push("sp").as("sp");
UnwindOperation unwindFp = Aggregation.unwind("fp");
UnwindOperation unwindSp = Aggregation.unwind("sp");
Aggregation aggregation = Aggregation.newAggregation(match, group, unwindFp, unwindSp);
AggregationResults<finalResults> results = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(R.class), FinalResults.class);