任务三:MangoDB java应用
15
常玲玲
开始于 2020-03-18 08:08
0 3 58
已截止

任务尚未发布或者你没有权限查看任务内容。

任务讨论
package mongodbwork;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;

public class MongoUtil {
	MongoCollection<Document> collection;
	public MongoUtil(String db,String collectionName) {
		//连接到mongodb服务器
		MongoClient mongoclient = new MongoClient("localhost",27017);
		//连接到数据库
		MongoDatabase mongodatabase = mongoclient.getDatabase(db);
		collection = mongodatabase.getCollection(collectionName);
		System.out.println(collection);
	}	
	
	
	public void insert(List list) {
		//清空集合后插入文档
		collection.drop();
		collection.insertMany(list);
		findAll();
		
	}
	
	public void updateOne(Bson filter,Document doc,Boolean upsert) {
      //筛选条件,更新文档,设置选项		
		UpdateResult re=collection.updateOne(filter, doc,new UpdateOptions().upsert(upsert));
		System.out.println(re.toString());
		findFilter(filter);
	}
	
	public void updateMany(Bson filter,Document doc,Boolean upsert) {
		UpdateResult re=collection.updateMany(filter, doc,new UpdateOptions().upsert(upsert));
		System.out.println(re.toString());
		findFilter(filter);
	}
	
	public void deleteOne(Bson filter) {	
		//删除一条符合条件文档
		DeleteResult re = collection.deleteOne(filter);
		System.out.println(re.toString());
		findAll();
	}
	
	public void deleteMany(Bson filter) {	
		//删除符合条件的所有文档
		DeleteResult re = collection.deleteMany(filter);
		System.out.println(re.toString());
	//	findFilter(filter);
		findAll();
	}
	
	public void findAll() {
		FindIterable<Document> find = collection.find();
		MongoCursor<Document> cursor = find.iterator();
		while(cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}
	
	
	public void findSortAll(Document doc) {
		FindIterable<Document> find = collection.find().sort(doc);
		MongoCursor<Document> cursor = find.iterator();
		while(cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}
	
	
	public void findFilter(Document doc) {	
		FindIterable<Document> find = collection.find(doc);
		MongoCursor<Document> cursor = find.iterator();
		while(cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}
	
	public void findFilter(Bson filter) {	
		FindIterable<Document> find = collection.find(filter);
		MongoCursor<Document> cursor = find.iterator();
		while(cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}
	
	
}
package mongodbwork;

import java.awt.dnd.DnDConstants;
import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.BasicDBObject;
import com.mongodb.client.model.Filters;

import mongodbwork.MongoUtil;

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MongoUtil mo = new MongoUtil("test","javatest");	
		
		//1.向集合javatest中插入如下数据
		List<Document> list = new ArrayList<Document>();	
		for (int i=1; i<=10; i++) {
			Document doc= new Document();
			doc.append("name", "zhangsan" + i);
			doc.append("sex", Math.round(Math.random() * 10) % 2);
	        doc.append("age", Math.round(Math.random() * 6) + 3);
	        doc.append("hobby","[]");  
	        list.add(doc);	
	    }			
		mo.insert(list);
		System.out.println("插入成功");
		
		//2、grade1中,追加zhangsan7`学生兴趣爱好为唱歌
		Bson filter2 = Filters.eq("name", "zhangsan7");
		Document docupdate2 = new Document("$set",new Document("hobby","[sing]"));
		mo.updateOne(filter2, docupdate2, true);
		System.out.println("追加兴趣爱好成功");
		
		//3、 给grade1所有学生的年龄都增加一岁
		Bson filter3 = Filters.exists("_id");
		Document docupdate3 = new Document("$inc",new Document("age",1));
		mo.updateMany(filter3, docupdate3, false);
		System.out.println("所有学生年龄+1成功");
		
		//4、grade1中删除zhangsan7学生的sex字段
		Bson filter4 = Filters.eq("name","zhangsan7");
		Document docdelete = new Document("$unset",new Document("sex",""));
		mo.updateOne(filter4, docdelete, false);
		System.out.println("删除zhangsan7的sex字段");
		
		//5、查看grade1的学生,按年纪升序
		Document doc5 =new Document("age",1);
		mo.findSortAll(doc5);		
		System.out.println("实现按年龄升序查询学生");
		
		
		//7、删除sex为0并且age大于5的学生
		Bson filter7 = Filters.and(Filters.eq("sex", 0),Filters.gt("age", 5));
		mo.deleteMany(filter7);
		System.out.println("成功删除符合条件的学生");
		

	}
}