MongoDB 3.0 for java 学习记录(一、java连接MongoDB)
2016-02-15 16:32
501 查看
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);
2、当MongoDB启动的时候添加--auth参数启动MongoDB(即安全认证模式下)
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
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);
2、当MongoDB启动的时候添加--auth参数启动MongoDB(即安全认证模式下)
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 安全权限访问控制
- mongodb java driver3.0初探
- mongodb简介与增删该查
- mongodb的sum操作以及javaapi的实现
- mongodb 主从搭建
- mysql , mongodb 备份
- mongodb安装
- mongodb用户管理
- MongoDB No1
- MongoDB的停止方式
- 学习MongoDB(三) Add an Arbiter to Replica Set 集群中加入仲裁节点
- 在Mac OS上安装使用MongoDB的教程
- 使用MongoDB Shell
- mongodb学习笔记
- Mongodb Expire Documents after a Specified Number of Seconds
- java操作MongoDB
- 在Mac OS上安装使用MongoDB的教程
- python-webpy和mongodb实现博客统计
- MongoDB的用法集锦:查询篇----->持续更新中
- Mac下安装和配置mongoDB