英文:
MongoDB raw java connection
问题
I am trying to create a generic mongo connection component that will be used with different mongo DB instances. I managed to make it work with some code:
// Creating a Mongo client
MongoClient mongo = new MongoClient("localhost", 27017);
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray());
System.out.println("Connected to the database successfully");
// Accessing the database
MongoDatabase database = mongo.getDatabase("myDb");
System.out.println("Credentials ::" + credential);
I don't understand why it needs to specify the database in 2 places: "myDb", once in the credential, and once while it does a "getDatabase". More than that on my setup I need to specify a different DB on "createCredential": "admin" in order to work. Why is the credential database different than the one I will run the query?
英文:
I am trying to create a generic mongo connection component that will be used with different mongo DB instances. I managed to make it work with some code like:
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Accessing the database
MongoDatabase database = mongo.getDatabase("myDb");
System.out.println("Credentials ::"+ credential);
I don't understand why it needs to specify the database in 2 places: "myDb"
, once in the credential, and once while it does a getDatabase
. More than that on my setup I need to specify a different DB on createCredential
: "admin" in order to work. Why is the credential database different than the one I will run the query?
答案1
得分: 1
当您深入检查代码时,您会发现以下令人信服的原因。
这是所有认证器失效的地方。
private void authenticateAll(final InternalConnection internalConnection, final ConnectionDescription connectionDescription) {
if (connectionDescription.getServerType() != ServerType.REPLICA_SET_ARBITER) {
for (final Authenticator cur : authenticators) {
cur.authenticate(internalConnection, connectionDescription);
}
}
}
authenticators
包含一组凭据。有四种实现方式。
- 默认
- 本机
- x509
- sasl
> "myDb",在凭证中出现一次 - 为什么
在这里指定的主要原因是,要在哪个数据库上执行认证命令,因为每个数据库可以有不同的用户名。
executeCommand(getCredential().getSource(), authCommand, connection);
> 而在执行 getDatabase
时出现一次 - 为什么
这完全不同。它返回包含 read、write concerns、collection 列表、创建视图、创建集合
等选项的 MongoDatabase
对象。
英文:
When you inspect the code deeper, you will find the below compelling reasons.
This is the place where all the authenticators falls down.
private void authenticateAll(final InternalConnection internalConnection, final ConnectionDescription connectionDescription) {
if (connectionDescription.getServerType() != ServerType.REPLICA_SET_ARBITER) {
for (final Authenticator cur : authenticators) {
cur.authenticate(internalConnection, connectionDescription);
}
}
}
authenticators
contains list of credentials. There are four implementations.
- Default
- Native
- x509
- sasl
>"myDb", once in the credential - why
Main reason to specify here, on which database the authenticate command has to be executed as each database can have different user name.
executeCommand(getCredential().getSource(), authCommand, connection);
> once while it does a getDatabase - why
It is altogether different. It returns MongoDatabase
object which contains options to read, write concerns, list of collections, create view, create collection
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论