javamongodbmorphia

connect to mongodb using Morphia in Java


I am trying to connect to MongoDb using Morphia in Java. I went through some tutorials as well as some question asked in stackoverflow. They all using "createDatastore" and this method is deprecated. There are two methods to create a datastore and both are deprected. What is the alternative to this method. The way I am using this as follows

    Morphia morphia = new Morphia();
    morphia.mapPackage("org.mongodb.morphia");
    final Datastore datastore = morphia.createDatastore(mongoClient, mapper, "equity");
    TestData testData = new TestData ();
    testData.setAvg(new BigDecimal("500.02"));
    testData.setDate(Date.valueOf("2021-08-20"));

    datastore.save(testData );

EDIT 1: enter image description here


Solution

  • It depends on what version you're using but with version 2.2, you would connect something like this:

    final Datastore datastore = Morphia.createDatastore(mongoClient, "equity");
    datastore.getMapper().mapPackage("org.mongodb.morphia");