+++++++ 20160404 begin+++++++
maven环境:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.2.2</version> </dependency>
+++++++ 20160404 end+++++++
1、当MongoDB没有添加使用auth认证方式的时候:
MongoClientURI uri = new MongoClientURI("mongodb://192.168.100.140:27017/local",MongoClientOptions.builder().cursorFinalizerEnabled(false));MongoClient client = new MongoClient(uri);MongoDatabase db=client.getDatabase("local");MongoCollection<Document> collection = db.getCollection("system.users");List<Document> foundDocument = collection.find().into(new ArrayList<Document>());System.out.println(foundDocument);
MongoClient client = null;ServerAddress serverAddress = new ServerAddress("192.168.100.140",27017);List<ServerAddress> seeds = new ArrayList<ServerAddress>();seeds.add(serverAddress);MongoCredential credentials = MongoCredential.createScramSha1Credential("admin", "admin", "admin".toCharArray());List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();credentialsList.add(credentials);client = new MongoClient(seeds, credentialsList);MongoDatabase db = client.getDatabase("admin");// MongoIterable<Document> collections=db.listCollections();MongoCollection<Document> collection = db.getCollection("system.users");List<Document> foundDocument = collection.find().into(new ArrayList<Document>());System.out.println(foundDocument);
mongoDB 3.0 安全权限访问控制:http://blog.csdn.net/gsying1474/article/details/47813059
by 刘迎光@萤火虫工作室
OpenBI交流群:495266201
MicroService 微服务交流群:217722918
mail: liuyg#liuyingguang.cn
博主首页(防止爬虫):http://blog.liuyingguang.cn
OpenBI问答社区:http://openbi.liuyingguang.cn/