@Test
public void insertDemo() {
User user = new User();
user.setUsername("cang");
user.setCountry("USA");
user.setAge(20);
user.setLenght(1.77f);
user.setSalary(new BigDecimal("6265.22"));
// 添加“address”子文档
Address address1 = new Address();
address1.setaCode("411222");
address1.setAdd("sdfsdf");
user.setAddress(address1);
// 添加“favorites”子文档,其中两个属性是数组
Favorites favorites1 = new Favorites();
favorites1.setCites(Arrays.asList("东莞", "东京"));
favorites1.setMovies(Arrays.asList("西游记", "一路向西"));
user.setFavorites(favorites1);
User user1 = new User();
user1.setUsername("chen");
user1.setCountry("China");
user1.setAge(30);
user1.setLenght(1.77f);
user1.setSalary(new BigDecimal("6885.22"));
Address address2 = new Address();
address2.setaCode("411000");
address2.setAdd("我的地址2");
user1.setAddress(address2);
Favorites favorites2 = new Favorites();
favorites2.setCites(Arrays.asList("珠海", "东京"));
favorites2.setMovies(Arrays.asList("东游记", "一路向东"));
user1.setFavorites(favorites2);
// 使用insertMany插入多条数据
doc.insertMany(Arrays.asList(user, user1));
}
3.3 查询
@Test
public void testFind() {
final List<User> ret = new ArrayList<>();
Block<User> printBlock = new Block<User>() {
@Override
public void apply(User t) {
System.out.println(t.toString());
ret.add(t);
}
};
// select * from users where favorites.cites has "东莞"、"东京"
Bson all = all("favorites.cites", Arrays.asList("东莞", "东京"));// 定义数据过滤器,喜欢的城市中要包含"东莞"、"东京"
FindIterable<User> find = doc.find(all);
find.forEach(printBlock);
logger.info("------------------>" + String.valueOf(ret.size()));
ret.removeAll(ret);
// select * from users where username like '%s%' and (contry= English or contry
// = USA)
String regexStr = ".*s.*";
Bson regex = regex("username", regexStr);// 定义数据过滤器,username like '%s%'
Bson or = or(eq("country", "English"), eq("country", "USA"));// 定义数据过滤器,(contry= English or contry = USA)
FindIterable<User> find2 = doc.find(and(regex, or));
find2.forEach(printBlock);
logger.info("------------------>" + String.valueOf(ret.size、()));
}
3.4 更新
@Test
public void testUpdate() {
// update users set age=6 where username = 'lison'
Bson eq = eq("username", "lison");// 定义数据过滤器,username = 'lison'
Bson set = set("age", 8);// 更新的字段.来自于Updates包的静态导入
UpdateResult updateMany = doc.updateMany(eq, set);
logger.info("------------------>" + String.valueOf(updateMany.getModifiedCount()));// 打印受影响的行数
// update users set favorites.movies add "小电影2 ", "小电影3" where favorites.cites
// has "东莞"
Bson eq2 = eq("favorites.cites", "东莞");// 定义数据过滤器,favorites.cites has "东莞"
Bson addEachToSet = addEachToSet("favorites.movies", Arrays.asList("小电影2 ", "小电影3"));// 更新的字段.来自于Updates包的静态导入
UpdateResult updateMany2 = doc.updateMany(eq2, addEachToSet);
logger.info("------------------>" + String.valueOf(updateMany2.getModifiedCount()));
}
3.5 删除
@Test
public void testDelete() {
// delete from users where username = ‘lison’
Bson eq = eq("username", "lison");// 定义数据过滤器,username='lison'
DeleteResult deleteMany = doc.deleteMany(eq);
logger.info("------------------>" + String.valueOf(deleteMany.getDeletedCount()));// 打印受影响的行数
// delete from users where age >8 and age <25
Bson gt = gt("age", 8);// 定义数据过滤器,age > 8,所有过滤器的定义来自于Filter这个包的静态方法,需要频繁使用所以静态导入
Bson lt = lt("age", 25);// 定义数据过滤器,age < 25
Bson and = and(gt, lt);// 定义数据过滤器,将条件用and拼接
DeleteResult deleteMany2 = doc.deleteMany(and);
logger.info("------------------>" + String.valueOf(deleteMany2.getDeletedCount()));// 打印受影响的行数
}