lookup与getmappedresults()一起使用时无法正常工作

1l5u6lss  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(414)

我有以下代码,其答案为 getRawResult 以及 getMappedResult 它们是不同的。我想知道怎样才能包括 user 以及 place mappedresults中的对象。

  1. // This methd searches the given string in reviews of the given country
  2. MatchOperation matchRegion = Aggregation.match(Criteria.where("Country").is("Germany"));
  3. //To match the review
  4. TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
  5. MatchOperation match = Aggregation.match(criteria);
  6. GroupOperation group = Aggregation.group("place.place_name").push("$$ROOT").as("reviews").sum("textScore")
  7. .as("score");
  8. ProjectionOperation project = Aggregation
  9. .project("some_point", "place", "user", "date")
  10. .andExpression("{$meta: \"textScore\"}").as("textScore");
  11. SortOperation sortByScore = Aggregation.sort(Sort.Direction.DESC, "score");
  12. Aggregation aggregation = Aggregation.newAggregation(match,Aggregation.lookup("place","placeId","_id","place"),
  13. Aggregation.unwind("place"),Aggregation.lookup("user","userId","_id","user"),
  14. Aggregation.unwind("user") , matchRegion, project, group, sortByScore);
  15. AggregationResults<ReviewAggrResults> results = mongoTemplate.aggregate(aggregation,
  16. mongoTemplate.getCollectionName(Review.class), ReviewAggrResults.class);
  17. List<ReviewAggrResults> result = results.getMappedResults();
  18. Object object = mongoTemplate.aggregate(aggregation, Review.class, Object.class).getRawResults();
  19. Print.print("result object rawResults", object);
  20. Print.print("result reviewaggrresult: ", result);

使用上述代码,打印的结果 getRawResults 以及 ReviewAggrResult 如下所示:
原始结果:

  1. {
  2. "results" : [ {
  3. "_id" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany",
  4. "reviews" : [ {
  5. "_id" : {
  6. "timestamp" : 1610817567,
  7. "date" : 1610817567000
  8. },
  9. "some_point" : "something",
  10. "date" : "Sat Jan 16 18:19:27 CET 2021",
  11. "place" : {
  12. "_id" : {
  13. "timestamp" : 1610817567,
  14. "date" : 1610817567000
  15. },
  16. "place_name" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany",
  17. "country" : "Germany",
  18. },
  19. "user" : {
  20. "_id" : {
  21. "timestamp" : 1610817549,
  22. "date" : 1610817549000
  23. },
  24. "firstname" : "test1first",
  25. "lastname" : "test1last",
  26. },
  27. "textScore" : 5.25
  28. } ],
  29. "score" : 5.25
  30. } ],
  31. "ok" : 1.0
  32. }

Map结果:

  1. [ {
  2. "reviews" : [ {
  3. "id" : "6003201f98afbc730e4f58eb",
  4. "some_point" : "something",
  5. "placeId" : null,
  6. "userId" : null,
  7. "date" : "Sat Jan 16 18:19:27 CET 2021",
  8. "textScore" : 5.25
  9. } ],
  10. "id" : "ibis Heidelberg, Willy-Brandt-Platz 3, 69115 Heidelberg, Germany",
  11. "userId" : null,
  12. "user" : null,
  13. "score" : 5.25
  14. } ]

为什么它们不同?原始结果是正确的,但是当它被Map时,place和user对象被移除。如何将它们也包含在mappedresult中?

bqf10yzr

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。你能做的就是

  1. class Place{
  2. ObjectId _id;
  3. String place_name;
  4. String country;
  5. //Constrtuctors, Getters, Setters
  6. }

在复习课上,

  1. class Review{
  2. // other fields
  3. Place place;
  4. }

您还需要对其他对象或数组执行相同的操作。这应该管用

展开查看全部

相关问题