public classMongoDBTest
{//定义一个客户端
private MongoClient client = null;//定义一个MongoDB数据库
private MongoDatabase database = null;
@Beforepublic voidinit()
{//连接MongoDB客户端
client = new MongoClient("127.0.0.1", 27017);//使用数据库mongoDataBase
database = client.getDatabase("mongoDataBase");
}/***
* @描述:向user集合中添加一条数据*/@Testpublic voidtestInsertOne()
{//获取user集合
MongoCollection collection = database.getCollection("user");
Document document= newDocument();
document.append("userName", "jack");
document.append("age", 23);
document.append("sex", "男");
collection.insertOne(document);
}/***
* @描述:向user集合中添加多条数据*/@Testpublic voidtestInsertMany()
{//获取user集合
MongoCollection userCollection = database.getCollection("user");//定义多个Document对象
Document document1 = new Document("userName", "lily");
document1.append("age", 24);
document1.append("sex", "男");
Document document2= new Document("userName", "mary");
document2.append("age", 30);
document2.append("sex", "女");
Document document3= new Document("userName", "小吴");
document3.append("age", 27);
document3.append("sex", "男");
userCollection.insertMany(Arrays.asList(document1, document2, document3));
}/***
* @描述:删除user集合中数据*/@Testpublic voidremove()
{//获取user集合
MongoCollection userCollection = database.getCollection("user");//执行删除的方法(删除age=23的所有人)//构造删除条件
Bson delBson = Filters.eq("age", 23);//一次性删除所有符合条件的数据(删除age=23的所有人)
userCollection.deleteMany(delBson);
}/***
* @描述:删除user集合中数据(带多个删除条件的)*/@Testpublic voidremove2()
{//获取users集合
MongoCollection userCollection = database.getCollection("user");//执行删除的方法(删除age>=20 并且 age<=30的所有人)//构造删除条件(age>=20)
Bson delBson1 = Filters.gte("age", 20);//构造删除条件(age<=30)
Bson delBson2 = Filters.lte("age", 30);//合并删除条件
Bson delBson =Filters.and(delBson1, delBson2);
userCollection.deleteMany(delBson);
}/***
* @描述:查询出user集合中所有数据*/@Testpublic voidtestGetAll2()
{//获取user集合
MongoCollection userCollection = database.getCollection("user");//查询所有文档
FindIterable allDocuments =userCollection.find();//获取迭代器
MongoCursor iterator =allDocuments.iterator();while(iterator.hasNext())
{
Document document=iterator.next();
System.out.println(document);
}
}/***
* @描述:有条件查询user集合数据*/@Testpublic voidtestGet()
{//获取users集合
MongoCollection userCollection = database.getCollection("user");//查询age>=25 并且age <=30的人
Bson queryBson = Filters.and(Filters.gte("age", 25), Filters.lte("age", 30));
FindIterable allDocuments =userCollection.find(Filters.and(queryBson));//获取迭代器
MongoCursor iterator =allDocuments.iterator();while(iterator.hasNext())
{
Document document=iterator.next();
System.out.println(document);
}
}/***
* @描述:分页查询出users集合中的数据*/@Testpublic voidtestGet2()
{//获取users集合
MongoCollection userCollection = database.getCollection("user");//查询前两个数据,skip(0):跳过0个,limit(2)查询2个
FindIterable allDocuments =userCollection.find();
FindIterable page = allDocuments.skip(0).limit(2);//获取迭代器
MongoCursor iterator =page.iterator();while(iterator.hasNext())
{
Document document=iterator.next();
System.out.println(document);
}
}/***
* @描述:修改操作*/@Testpublic voidupdate()
{//获取user集合
MongoCollection userCollection = database.getCollection("user");修改 将userName为"lily" 的age改为25//db.users.update({"userName":"lily"},{$set:{"age":25}});//条件
Bson eqBson = Filters.eq("userName", "lily");//要修改的内容
Document updateBson = new Document("$set", new Document("age", 25));
UpdateResult updateMany=userCollection.updateMany(eqBson, updateBson);
System.out.println(updateMany.toString());
}
@Afterpublic voidclose()
{
client.close();
}
}