我有以下代码,其答案为 getRawResult
以及 getMappedResult
它们是不同的。我想知道怎样才能包括 user
以及 place
mappedresults中的对象。
// This methd searches the given string in reviews of the given country
MatchOperation matchRegion = Aggregation.match(Criteria.where("Country").is("Germany"));
//To match the review
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
MatchOperation match = Aggregation.match(criteria);
GroupOperation group = Aggregation.group("place.place_name").push("$$ROOT").as("reviews").sum("textScore")
.as("score");
ProjectionOperation project = Aggregation
.project("some_point", "place", "user", "date")
.andExpression("{$meta: \"textScore\"}").as("textScore");
SortOperation sortByScore = Aggregation.sort(Sort.Direction.DESC, "score");
Aggregation aggregation = Aggregation.newAggregation(match,Aggregation.lookup("place","placeId","_id","place"),
Aggregation.unwind("place"),Aggregation.lookup("user","userId","_id","user"),
Aggregation.unwind("user") , matchRegion, project, group, sortByScore);
AggregationResults<ReviewAggrResults> results = mongoTemplate.aggregate(aggregation,
mongoTemplate.getCollectionName(Review.class), ReviewAggrResults.class);
List<ReviewAggrResults> result = results.getMappedResults();
Object object = mongoTemplate.aggregate(aggregation, Review.class, Object.class).getRawResults();
Print.print("result object rawResults", object);
Print.print("result reviewaggrresult: ", result);
使用上述代码,打印的结果 getRawResults
以及 ReviewAggrResult
如下所示:
原始结果:
{
"results" : [ {
"_id" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany",
"reviews" : [ {
"_id" : {
"timestamp" : 1610817567,
"date" : 1610817567000
},
"some_point" : "something",
"date" : "Sat Jan 16 18:19:27 CET 2021",
"place" : {
"_id" : {
"timestamp" : 1610817567,
"date" : 1610817567000
},
"place_name" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany",
"country" : "Germany",
},
"user" : {
"_id" : {
"timestamp" : 1610817549,
"date" : 1610817549000
},
"firstname" : "test1first",
"lastname" : "test1last",
},
"textScore" : 5.25
} ],
"score" : 5.25
} ],
"ok" : 1.0
}
Map结果:
[ {
"reviews" : [ {
"id" : "6003201f98afbc730e4f58eb",
"some_point" : "something",
"placeId" : null,
"userId" : null,
"date" : "Sat Jan 16 18:19:27 CET 2021",
"textScore" : 5.25
} ],
"id" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany",
"userId" : null,
"user" : null,
"score" : 5.25
} ]
为什么它们不同?原始结果是正确的,但是当它被Map时,place和user对象被移除。如何将它们也包含在mappedresult中?
1条答案
按热度按时间bqf10yzr1#
getMappedResults()
提供到Map类和聚合结果的直接Map。看看这里怎么了。当你采取getRawResult()
,我只举一个例子place
"place" : { "_id" : { "timestamp" : 1610817567, "date" : 1610817567000 }, "place_name" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany", "country" : "Germany", }
place
是一个具有_id
,place_name
以及country
. 但在你的班级里Review
,你只有placeId
不是由结果提供的。所以它不能做点对点的Map。你能做的就是在复习课上,
您还需要对其他对象或数组执行相同的操作。这应该管用