本文介绍了MongoDb 和 morphia 密码和用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

玩弄这个而无法掌握
关于如何使用密码连接.

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

在 MongoDb 中很容易理解,但是当我使用吗啡时却不是
很明显,因为文档很少,或者我错过了.

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.

我已经有一个包含 5 个文档的集合.
我可以申请一个用户/通行证吗?

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?

从这篇文章中获取代码(感谢@Lennart Koopmann)
MongoDB Java 驱动教程

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;
        }
    }

推荐答案

createDatastore(m, DB_NAME, username, password)

这个重载方法已经被引入此处.

也许您使用的是旧版 Moprhia 客户端?

Maybe you are using older Moprhia client?

这篇关于MongoDb 和 morphia 密码和用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 06:24
查看更多