java 连接 MOngoDB 操作

package mongo;
import java.nio.file.DirectoryStream.Filter;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.MongoCursor;
public class Mongo1 {
	public static void main(String args []){ 
	      try {    
	       //连接到mongodb服务
	         MongoClient mongoClient = new MongoClient ("localhost",27017); 

	    	  //有密码的连接
	    	  /*
	        ServerAddress  serverAddress = new  ServerAddress("localhost",27017);
	        ArrayList addrs = new ArrayList(); 
	        addrs.add(serverAddress);
	        
	        MongoCredential credential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray()); 
	        ArrayList credentials1 = new ArrayList();
	        credentials1.add(credential);
	        
//	        //连接认证获取MOngoDB连接
	        MongoClient mongoClient = new MongoClient(addrs,credentials1);	*/
	    	
	    	  
	    	  //连接到数据库test	       
	        MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
	        System.out.println("成功连接到数据库"); 
	        System.out.println("数据库:"+mongoDatabase.getName()); 
	        
	        //指定集合,插入文档  
	         /** 
	         * 1. 创建文档 org.bson.Document 参数为key-value的格式 
	         * 2. 创建文档集合List 
	         * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List) 插入单个文档可以用 mongoCollection.insertOne(Document) 
	         
	        MongoCollection mongoCol= mongoDatabase.getCollection("myinsert");
	        System.out.println("集合myinsert选择成功");
	        Document document1 = new Document("name","刘备")
	        .append("_id", 5)
	        .append("house","重庆")
	    	.append("Ares","78km2");
	        Document document2 = new Document("name","关羽")
	    	        .append("_id", 6)
	    	        .append("house","汉")
	    	    	.append("Ares","960km2");
	        Document document3 = new Document("name","曹操")
	    	        .append("_id", 7)
	    	        .append("house","魏国")
	    	    	.append("Ares","300km2");
	        List documents = new ArrayList();  
	        documents.add(document1);  
	        documents.add(document2); 
	        documents.add(document3); 
	        mongoCol.insertMany(documents);
	        System.out.println("成功插入");
	        * */
	        MongoCollection<org.bson.Document> mongoCol= mongoDatabase.getCollection("myinsert");  
	        System.out.println("集合myinsert选择成功");
	        
	        mongoCol.updateMany(Filters.eq("name","曾啊牛"), new Document("$set",new Document("name","周瑜")));
	        System.out.println("数据更新成功");
	        
	        //删除符合条件的第一个文档  
//	         mongoCol.deleteOne(Filters.eq("likes", 200));  
	         //删除所有符合条件的文档  
	         mongoCol.deleteMany (Filters.eq("Ares","100km2"));
	         System.out.println("删除成功");
	        
	        //检索所有文档  
	         /** 
	         * 1. 获取迭代器FindIterable 
	         * 2. 获取游标MongoCursor 
	         * 3. 通过游标遍历检索出的文档集合 
	         * */  
	         FindIterable<Document> findIterable = mongoCol.find();  
	         MongoCursor<Document> mongoCursor = findIterable.iterator();
	         System.out.println("文档搜索:");
	         while(mongoCursor.hasNext()){  
	            System.out.println(mongoCursor.next());  }
	        
	      } catch(Exception e){ 
	        System.err.println(e.getClass().getName()+":" + e.getMessage()); 
	     } 
	   } 
	}

java 连接 MOngoDB 操作_第1张图片

你可能感兴趣的:(NOsql——MongoDB,Redis,mongodb,java)