I have the following Mongo configuration in a Java Spring project:
@Configuration
public class MongoConfiguration {
public static int allocateRandomPort() {
try {
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
server.close();
return port;
} catch (IOException e) {
throw new RuntimeException("Failed to acquire a random free port", e);
}
}
@Bean
public Mongo mongo() throws IOException {
System.setProperty("DB.TRACE", "true");
return new EmbeddedMongoBuilder()
.version("2.6.0")
.bindIp("127.0.0.1")
.port(allocateRandomPort())
.build();
}
}
To start building queries using the Querydsl Mongodb module, the documentation says that:
Morphia morphia;
Datastore datastore;
// ...
QUser user = new QUser("user");
MorphiaQuery<User> query = new MorphiaQuery<User>(morphia, datastore, user);
I have no ideia on how to create the instance of the MorphiaQuery class. Should the Datastore object be a inject Mongo instance? And how about Morphia object? Other tutorials I found online create the instance as:
Morphia morphia = new Morphia()
.map(Book.class, Author.class, Tag.class);
I would like to build the queries inside a Spring Data MongoDB repository. Is there a fully functional example somewhere?
Thanks.