2

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.

1 Answer 1

1

Morphia and Spring Data Mongodb are alternative object mappers for Querydsl Mongodb. Querydsl itself bundles with the Morphia integration, for Spring Data Mongodb you will need to refer to the references docs to set it up.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended using this tutorial, which proved extremely useful: petrikainulainen.net/programming/spring-framework/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.