Search code examples
mongodbmorphia

MongoDb and morphia password and username


playing around with this and cannot get a grip
on how to connect with password.

In MongoDb it's quite easy to understand but when i use morphia it's not
so quite obvious because documentation is minimal or i missed it.

I already have a Collection with 5 documents.
Can i apply a user/pass on that one?

How can i modify this code so logging uses a username and password?

Took the code from this post (Thanks @Lennart Koopmann)
MongoDB Java driver tutorial

public final class MongoConnectionManager {

 private static final MongoConnectionManager
 INSTANCE = new MongoConnectionManager();
 private final Datastore db;
 public static final String DB_NAME = "mongo_database";

 private MongoConnectionManager() {
            try {
                Mongo m = new Mongo("localhost", 27017);
            db = new Morphia().map(UserData.class)
     .map(Statistic.class)
     .map(FriendList.class)
     .map(ServerData.class)
     .map(BatchData.class).createDatastore(m, DB_NAME);

                db.ensureIndexes();
            }
            catch (Exception e) {
                throw new RuntimeException("Error initializing mongo db", e);
            }
        }

        public static MongoConnectionManager instance() {
            return INSTANCE;
        }

        public Datastore getDb() {

            return db;
        }   
    }

Solution

  • createDatastore(m, DB_NAME, username, password)
    

    This overloaded method has been introduced here.

    Maybe you are using older Moprhia client?