Exception in thread "main" com.mongodb.CommandResult$CommandFailure: command failed [command failed [listDatabases] { "serverUsed" : "192.168.100.77:27017" , "ok" : 0.0 , "errmsg" : "not authorized on admin to execute command { listDatabases: 1 }" , "code" : 13} at com.mongodb.CommandResult.getException(CommandResult.java:75) at com.mongodb.CommandResult.throwOnError(CommandResult.java:121) at com.mongodb.Mongo.getDatabaseNames(Mongo.java:328) at com.fz.test.SimpleTest.main(SimpleTest.java:16)
package com.fz.test; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.util.JSON; public class SimpleTest { public static void main(String[] args) throws UnknownHostException, MongoException { Mongo mg = new Mongo("192.168.2.77", 27017); // 查询所有的Database for (String name : mg.getDatabaseNames()) { System.out.println("dbName: " + name); } DB db = mg.getDB("test"); // 查询所有的聚集集合 for (String name : db.getCollectionNames()) { System.out.println("collectionName: " + name); } DBCollection users = db.getCollection("users"); // 查询所有的数据 DBCursor cur = users.find(); while (cur.hasNext()) { System.out.println(cur.next()); } System.out.println(cur.count()); System.out.println(cur.getCursorId()); System.out.println(JSON.serialize(cur)); } }
MongoDBPlugin可能java上最好用而且实时跟进官方版本的ORM,让您更加舒适地使用MongoDB。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。