Tag - MongoDB

MongoDB java    2017-07-19 22:51:21    804
+++++++ 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);

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".toChar