【发布时间】:2014-09-26 23:11:29
【问题描述】:
我已经为我的研究安装了 MongoDB,并创建了数据库:mydb、jobs
我创建了一个用户:
db.createUser( { user: "devdbuser", pwd: "123456", roles: [ "readWrite" ] } )
然后在我的 java 代码中我正在尝试:
MongoClient client = new MongoClient(Arrays.asList(new ServerAddress[] { new ServerAddress("localhost", 27017)}),
Arrays.asList(new MongoCredential[] { MongoCredential.createMongoCRCredential("devdbuser", "mydb", "123456".toCharArray()) } ));
DB myDB = client.getDB("jobs"); // Here I get the jobs DB with user I created for 'mydb'
DBCollection collection = myDB.getCollection("myfirstcollection");
collection.insert((DBObject)JSON.parse("{\"name\": \"First user\", \"email\": \"first_user@mail.com\"}"));
client.close();
请注意,当我创建 MongoClient 时,我请求连接到“mydb”,并为创建的用户提供凭据。 但是当我使用作业数据库并尝试将数据插入集合时,一切正常。
我希望用户没有权限的错误,我错过了什么吗?
感谢您的帮助。
【问题讨论】:
标签: mongodb mongodb-java