We use Spring Data mongodb version 2.1.3.
We have two collections cities
and countries
.
@Document
public class City {
private String name;
private ObjectId id;
private ObjectId countryId;
}
@Document
public class Country {
private String name;
private ObjectId id;
}
We have db repositories extending org.springframework.data.mongodb.repository.MongoRepository
.
Now we want to make REST API end-points which would return aggregated data CityCountryDto
.
For this reason we want to make read-only view cityCountry
https://docs.mongodb.com/manual/reference/method/db.createView/
db.createView (
"cityCountry",
"city",
[
{ $lookup: { from: "country", localField: "countryId", foreignField: "id", as: "country_docs" } },
{ $project: { "country_docs._id": 0} }
]
)
We tried
@Document
CityCountry extends City {
private Country country;
}
Note, in order it to work, we had either to create the view before we run the application in the database or we had to use data change log tool (like Flyway or liquibase or mongobee). To make things easier, consider that the view existed in the database before we start the application.
My question, is there Spring data support view out of box? Is there a better way to achieve this?