Hi,
I’m using Morphia 1.6 as we use Java 8 at work.
I need to join two documents using an aggregation and then project just the items I want from both documents.
e.g.
I want to process a list of books and their authors (where author in Book is the _id of Person), and list the book title, author name and age.
Book = {
_id,
title,
author,
isbn
}
Person = {
_id,
name,
age,
website
}
This is what I’ve got to just dump the values into json:
Iterator bookAggregate = datastore.createAggregation(Book.class)
.sort(descending("title"))
.lookup("Person", "author", "_id", "bookAuthor")
.project(projection("_id"),
projection("title"),
projection("bookAuthor.name"),
projection("bookAuthor.age"))
.aggregate(Book.class);
while (bookAggregate.hasNext()){
ObjectMapper om = new ObjectMapper();
try {
json = om.writerWithDefaultPrettyPrinter().writeValueAsString(bookAggregate.next());
System.out.println(json);
} catch (Exception ex) {
}
}
When I view the json string, any items from Person (name & age) are not there. Only the items from Book are output to the json.
Apologies if this is a poor example, but how do I include items from the “joined” table using project?
Thanks
Steve