也就是说SpringBoot2.0之后,采用了新的mongodb驱动(新的访问数据方式)mongodb-driver-3.6.3.jar,而SpringBoot1.X版本用的是mongodb3.4.3及以前的驱动,至少目前来看SpringBoot-1.5.19版本是这样的。
{
"_id" : 8,
"name" : "小灰灰",
"age" : 26,
"sex" : "男",
"university" : { //毕业的学校
"name" : "北京大学",
"address" : "北京"
},
"workExperience" : [{ //工作经历
"startDate" : "2010-10-10",
"endDate" : "2013-06-06",
"address" : "深圳",
"name" : "腾讯"
}]
}
dao层代码
/**
* 没有save方法
* @param document
*/
public void insertOne(Document document){
mongoTemplate.getCollection(CollectionName).insertOne(document);
}
public void insertMany(List<Document> documents){
mongoTemplate.getCollection(CollectionName).insertMany(documents);
}
测试代码
/**
* 插入一条数据
*/
@Test
public void insertOneTest(){
Document document = new Document();
document.put("_id",1);
document.put("name","小明");
document.put("age",25);
document.put("sex","男");
documentQueryDao.insertOne(document);
}
/**
* 插入多条数据
*/
@Test
public void saveManyTest(){
Document document = new Document();
document.put("_id",6);
document.put("name","小赵");
document.put("age",25);
document.put("sex","男");
Document document2 = new Document();
document2.put("_id",7);
document2.put("name","小静");
document2.put("age",22);
document2.put("sex","女");
List<Document> list=new ArrayList<>();
list.add(document);
list.add(document2);
documentQueryDao.insertMany(list);
}
这个版本查询多条数据,返回的结果集是FindIterable可迭代对象,并且不能直接转成List对象。要自己循环转。一下示例,体会下。跟MySQL的JDBC查询返回的结果集很相似。
dao层代码
/**
* 根据条件查询所有符合条件的结果
* @param Document
* @return
*/
// public FindIterable find(Document Document){
// return mongoTemplate.getCollection(CollectionName).find(Document);
// }
public FindIterable<Document> find(Document Document){
return this.find(Document,null);
}
/**
* 根据条件查询所有符合条件的结果,并指定返回字段
* @param query
* @param projection
* @return
*/
public FindIterable<Document> find(Document query, Document projection){
return mongoTemplate.getCollection(CollectionName).find(query).projection(projection);
}
/**
* 分页查询
* @param query
* @param skip
* @param limit
* @return
*/
public FindIterable<Document> findPage(Document query,int skip,int limit){
return mongoTemplate.getCollection(CollectionName).find(query).skip(skip).limit(limit);
}
/**
* 查询条数
* @param query
* @return
*/
public long count(Document query){
return mongoTemplate.getCollection(CollectionName).count(query);
}
测试代码
/**
* 查询符合条件的一条数据
*/
@Test
public void findOneTest(){
Document document = new Document();
//document.put("_id",2);
document.put("name","小花");
Document result=documentQueryDao.findOne(document);
System.out.println(result);
}
/**
* 查询符合条件的多条数据
*/
@Test
public void findManyTest(){
Document document = new Document();
document.put("sex","男");
FindIterable<Document> iterable=documentQueryDao.find(document);
MongoCursor<Document> mongoCursor =iterable.iterator();
while (mongoCursor.hasNext()){
Document result=mongoCursor.next();
System.out.println(result);
}
}
/**
* 查询符合条件的数据,指定返回字段
*/
@Test
public void findManyWithFieldTest(){
Document document = new Document();
document.put("sex","男");
Document fieldDocument=new Document();
fieldDocument.put("_id",false);
fieldDocument.put("age",true);
FindIterable<Document> iterable=documentQueryDao.find(document,fieldDocument);
MongoCursor<Document> mongoCursor =iterable.iterator();
while (mongoCursor.hasNext()){
Document result=mongoCursor.next();
System.out.println(result);
}
}
/**
* 查询多条数据,分页
*/
@Test
public void findManyWithPageTest(){
Document document = new Document();
FindIterable<Document> iterable=documentQueryDao.findPage(document,0,5);
MongoCursor<Document> mongoCursor =iterable.iterator();
while (mongoCursor.hasNext()){
Document result=mongoCursor.next();
System.out.println(result);
}
}
/**
* $in 查询
* 反向 $nin
*/
@Test
public void findManyWithInTest(){
Document document = new Document();
document.put("_id",new Document().append("$in", Arrays.asList(1,2,3)));
FindIterable<Document> iterable=documentQueryDao.find(document);
MongoCursor<Document> mongoCursor = iterable.iterator();
while (mongoCursor.hasNext()){
Document result = mongoCursor.next();
System.out.println(result);
}
}
/**
* or 查询
*/
@Test
public void findManyWithOrTest(){
Document document1 = new Document();//条件1
document1.put("sex" , "女");
Document document2 = new Document();//条件2
document2.put("age" , 22);
Document dbObject = new Document();
dbObject.put("$or",Arrays.asList(document1,document2));//组合
FindIterable iterable = documentQueryDao.find(dbObject);
MongoCursor<Document> mongoCursor = iterable.iterator();
while (mongoCursor.hasNext()){
Document result = mongoCursor.next();
System.out.println(result);
}
}
/**
* $gt:大于
* $lt:小于
* $gte:大于等于
* $lte:小于等于
* $ne:不等于
*/
@Test
public void findManyWithGtTest(){
Document document = new Document();
//document.put("age" , new Document().append("$gt",22));
document.put("age" , new Document().append("$gt",22).append("$lt",24));
FindIterable iterable = documentQueryDao.find(document);
MongoCursor<Document> mongoCursor = iterable.iterator();
while (mongoCursor.hasNext()){
Document result = mongoCursor.next();
System.out.println(result);
}
}
/**
* 模糊查询
* mongodb用的是正则表达式来进行模糊查询的
*
* Spring中不区分大小写的模糊查询:
* //完全匹配
* Pattern pattern = Pattern.compile("^小$", Pattern.CASE_INSENSITIVE);
* //右匹配
* Pattern pattern = Pattern.compile("^.*小$", Pattern.CASE_INSENSITIVE);
* //左匹配
* Pattern pattern = Pattern.compile("^小.*$", Pattern.CASE_INSENSITIVE);
* //模糊匹配
* Pattern pattern = Pattern.compile("^.*小.*$", Pattern.CASE_INSENSITIVE);
*/
@Test
public void findManyWithPatternTest(){
String name ="花";
Pattern pattern=Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE);
Document document = new Document();
document.put("name" , pattern);
FindIterable iterable = documentQueryDao.find(document);
MongoCursor<Document> mongoCursor = iterable.iterator();
while (mongoCursor.hasNext()){
Document result = mongoCursor.next();
System.out.println(result);
}
}
/**
* 统计条数
*/
@Test
public void countTest(){
Document document = new Document();
document.put("sex","男");
long count=documentQueryDao.count(document);
System.out.println(count);
}
3.6版本的删除分俩个方法
删除一个 deleteOne
删除多个 deleteMany
dao层代码
/**
* 删除一条
* @param query
*/
public void removeOne(Document query){
mongoTemplate.getCollection(CollectionName).deleteOne(query);
}
/**
* 删除多条
* @param query
*/
public void removeMany(Document query){
mongoTemplate.getCollection(CollectionName).deleteMany(query);
}
测试代码
/**
* 删除一条
*/
@Test
public void removeOneTest(){
Document document = new Document();
document.put("_id",1);
documentQueryDao.removeOne(document);
}
/**
* 删除多条
*/
@Test
public void removeManyTest(){
Document document = new Document();
document.put("_id",new Document().append("$in",Arrays.asList(1,2)));
documentQueryDao.removeMany(document);
}
3.6版本的修改
修改一条 默认修改存在的数据 upsert是false
修改数据 不存在则添加 upsert设置为true:
mongoTemplate.getCollection(CollectionName).updateOne(query,update,new UpdateOptions().upsert(true));
修改多条数据 同上
dao层代码
/**
* 修改一条
* @param query
* @param update
*/
public void updateOne(Document query,Document update){
mongoTemplate.getCollection(CollectionName).updateOne(query,update);
}
/**
* 修改多条
* @param query
* @param update
*/
public void updateMany(Document query,Document update){
mongoTemplate.getCollection(CollectionName).updateMany(query,update);
}
/**
* 替换 整个文档
* @param query
* @param update
*/
public void replaceOne(Document query,Document update){
mongoTemplate.getCollection(CollectionName).replaceOne(query,update);
}
测试代码
/**
* 修改一条数据
*/
@Test
public void updateOneTest(){
Document document = new Document();
document.put("_id",2);
Document updateDocument = new Document();
updateDocument.put("name","test");
Document setDocument = new Document();
setDocument.put("$set",updateDocument);
documentQueryDao.updateOne(document,setDocument);
}
/**
* 修改多条数据
*
*
* 把名字是test的年龄都改成25岁
*/
@Test
public void updateManyTest(){
Document document = new Document();
document.put("name","test");
Document updateDocument = new Document();
updateDocument.put("age",25);
Document setDocument = new Document();
setDocument.put("$set",updateDocument);
documentQueryDao.updateMany(document,setDocument);
}
/**
* 修改一条数据 修改文档中的对象
*/
@Test
public void updateOneInObjectTest(){
Document document = new Document();
document.put("_id",2);
Document updateDocument = new Document();
updateDocument.put("name","北京大学");
updateDocument.put("address","北京");
Document setDocument = new Document();
setDocument.put("$set",new Document().append("university",updateDocument));
documentQueryDao.updateOne(document,setDocument);
}
/**
* 向数组中添加一个对象
* 也是更新文档的内容 所以算update
*/
@Test
public void updateOnePushListTest(){
Document document = new Document();
document.put("_id",2);//查询条件
Document pushDocument = new Document();//要插入的内容
pushDocument.put("startDate","2013-08-10");
pushDocument.put("endDate","2017-09-06");
pushDocument.put("address","深圳");
pushDocument.put("name","百度");
Document updateDocument = new Document();//要插入哪个list
updateDocument.put("workExperience",pushDocument);
Document setDocument = new Document();
setDocument.put("$push",updateDocument);//操作
documentQueryDao.updateOne(document,setDocument);
}
/**
* 修改一条数据 修改文档数组中的对象
*/
/*
{
"_id" : 2,
"name" : "test",
"age" : 25,
"university" : {
"name" : "北京大学",
"address" : "北京"
},
"workExperience" : [
{
"startDate" : "2013-08-10",
"endDate" : "2017-09-06",
"address" : "深圳", //把深圳改为北京
"name" : "百度"
}
]
}
*/
@Test
public void updateOneInListTest(){
Document document = new Document();
document.put("_id",2);
Document elemMatchDocument=new Document();
elemMatchDocument.put("name","百度");
document.put("workExperience",new Document().append("$elemMatch",elemMatchDocument));//不能用put?
//Document result=documentQueryDao.findOne(document); 查询符合条件的数组
//以上为查询条件
Document updateDocument = new Document();
updateDocument.put("workExperience.$.address","北京");
Document setDocument = new Document();
setDocument.put("$set",updateDocument);
documentQueryDao.updateOne(document,setDocument);
}
/**
* replace 替换:
* 把符合条件的document换成replaceDocument
* 不管以前document以前的内容是什么,
* 替换之后
* replaceDocument是什么 数据库里面就是什么。
* 这样理解:就是把符合条件的document删除,插入replaceDocument。
*
*/
@Test
public void replaceOneTest(){
Document document = new Document();
document.put("_id",1);
Document replaceDocument=new Document();
replaceDocument.put("name","小文");
replaceDocument.put("age",22);
replaceDocument.put("sex","女");
documentQueryDao.replaceOne(document,replaceDocument);
}