Guest User

Untitled

a guest
Jun 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. Aggregation aggregation = newAggregation(
  2. match(Criteria.where("idOfUser").is(loggedInAccount.getId())),
  3. group("imgID"),
  4. new CustomAggregationOperation(
  5. new BasicDBObject("$lookup",
  6. new BasicDBObject("from","img")
  7. .append("localField","_id")
  8. .append("foreignField","_id")
  9. .append("as","uniqueImgs")
  10. )
  11. ),
  12. limit(pageable.getPageSize()),
  13. skip(pageable.getPageSize()*pageable.getPageNumber())
  14. );
  15.  
  16. AggregationResults aggregationResults = mongo.aggregate(aggregation, "comment", String.class); //Using String at the moment just to see the output clearly.
  17.  
  18. public class CustomAggregationOperation implements AggregationOperation {
  19. private DBObject operation;
  20.  
  21. public CustomAggregationOperation (DBObject operation) {
  22. this.operation = operation;
  23. }
  24.  
  25. @Override
  26. public DBObject toDBObject(AggregationOperationContext context) {
  27. return context.getMappedObject(operation);
  28. }
  29. }
  30.  
  31. [{ "_id" : "570e2f5cb1b9125510a443f5" , "uniqueImgs" : [ ]}]
  32.  
  33. LookupOperation lookupOperation = LookupOperation.newLookup()
  34. .from("img")
  35. .localField("_id")
  36. .foreignField("_id")
  37. .as("uniqueImgs");
  38.  
  39. Aggregation agg = newAggregation(
  40. match(Criteria.where("idOfUser").is(loggedInAccount.getId())),
  41. group("imgID"),
  42. lookupOperation,
  43. limit(pageable.getPageSize()),
  44. skip(pageable.getPageSize()*pageable.getPageNumber())
  45. );
  46.  
  47. AggregationResults aggregationResults = mongo.aggregate(agg, "comment", String.clas);
  48.  
  49. db.country.aggregate([{ $lookup: { from: "state", localField: "countryId", foreignField: "countryId", as: "address"}}])
  50.  
  51. My following code in java ,however, returns an empty list .
  52. LookupOperation lookUpOprn = LookupOperation.newLookup()
  53. .from("state")
  54. .localField("countryId")
  55. .foreignField("countryId")
  56. .as("address");
  57. List<BasicDBObject> list = mongoTemplate.aggregate(aggregation, "country", BasicDBObject.class).getMappedResults();
  58.  
  59. Can you please help
Add Comment
Please, Sign In to add comment