JAVA封装Mongodb操作之⼆:MongoOperations mongoOperation相对于MongoRepository来说,⽅法更多⼀些,也更接近于mongo的原⽣态语⾔。它的⽅法包括了MongoRepository的所有⽅法外,还有分组查询group(),update⽅法,mapreduce⽅法以及直接操作collection的⽅法;好多都是⽅法的参数⾥都有query参数。
体类ur如下:
package com.mkyong.ur;
public class Ur {
private String id;
private String firstname;
private String lastname;
private int age;
//getter and tter methods
}
接下来,我们看具体的操作代码,如下,这⾥假设要将ur类保存到名为urprofile的数据集中。
小苏打是什么
package ;
import java.util.List;
import t.ApplicationContext;
import t.annotation.AnnotationConfigApplicationContext;
import t.support.GenericXmlApplicationContext;
import org.springframework.db.MongoOperations;
import org.springframework.db.query.Criteria;
import org.springframework.db.query.Query;
import org.springframework.db.query.Update;
import fig.SpringMongoConfig;
import com.mkyong.ur.Ur;
public class App
{
public static void main( String[] args )
{
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig. class );
MongoOperations mongoOperation = (Bean( " mongoTemplate " );
Ur ur = new Ur( " 1001 " , " yong " , " mook kim " , 30 );
//保存
mongoOperation.save( " urprofile " ,ur);
/
/查找
黄芪颗粒说明书Ur savedUr = mongoOperation.findOne( " urprofile " ,
new Query(Criteria.where( " id " ).is( " 1001 " )),
Ur. class );
System.out.println( " savedUr : " + savedUr);
//更新
mongoOperation.updateFirst( " urprofile " ,
new Query(Criteria.where( " firstname " ).is( " yong " )),
Update.update( " lastname " , " new lastname " ));
Ur updatedUr = mongoOperation.findOne( " urprofile " ,
new Query(Criteria.where( " id " ).is( " 1001 " )),
Ur. class );
System.out.println( " updatedUr : " + updatedUr);
//删除
new Query(Criteria.where( " id " ).is( " 1001 " )),
Ur. class );
//显⽰当前列表
System.out.println( " Number of ur = " + listUr.size());
}
}
输出结果如下:
savedUr : Ur [id = 1001 , firstname = yong, lastname = mook kim, age = 30 ]
updatedUr : Ur [id = 1001 , firstname = yong, lastname = new lastname, age = 30 ]
Number of ur =
Spring mongodb插⼊数据
下⾯详细讲解如何使⽤spring mongodb插⼊数据。在spring mongodb中,插⼊数据到
mongodb有如下⼏种⽅法:
Ur ur = new Ur( " ... " );
//将ur对象保存到"ur"这个collection中
mongoOperation.save(ur);
//将ur对象保存到"new collection"这个collection中
mongoOperation.save( " new collection " ,ur);
//将ur对象保存到"ur"这个collection中
mongoOperation.inrt(ur);
//将ur对象保存到"new collection"这个collection中
mongoOperation.inrt( " new collection " , ur);
//将ur的对象列表(List)保存到"ur"collection中去
mongoOperation.inrtList(urInList);
//将ur的对象列表(List)保存到"new collection"collection中去
mongoOperation.inrtList( " new collection " , urInList);
要注意的是,Spring mongodb中,当没有指定collection时,就会把对象保存到以对象命名的collection中。⽐如上例中的mongoOperation.inrt(ur),由于没指定collection的名称,所以会把ur对象保存到ur这个新建⽴的collection中。
另外请注意其中的save和inrt的区别。它们的区别为:
1)save意思是,当记录不存在时插⼊,或者是当记录已存在是更新,实际上就是saveorupdate的意思。
2) inrt的意思是:当记录不存在时插⼊,⽽如果记录存在时则忽略,继续插⼊。
下⾯举例⼦说明:
package ;
import java.util.ArrayList;
import java.util.List;
import t.ApplicationContext;
import t.annotation.AnnotationConfigApplicationContext;
import org.springframework.db.MongoOperations;
import org.springframework.db.query.Criteria;
import org.springframework.db.query.Query;
import fig.SpringMongoConfig;
import com.mkyong.ur.Ur;
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringMongoConfig. class );
MongoOperations mongoOperation = (MongoOperations) ctx
.getBean( " mongoTemplate " );
//新增⼀个ur对象,并把它放到"ABC"这个collection中
System.out.println( " " );
Ur urA = new Ur( " 1111 " , " ur " , " A " , 99 );
mongoOperation.save( " ABC " , urA);
Ur urA1 = mongoOperation.findOne( " ABC " ,
new Query(Criteria.where( " id " ).is( " 1111 " )), Ur. class );
System.out.println(urA1);
//插⼊新的ur,放到urB这个collection中去
取消流量套餐System.out.println( " " );
Ur urB = new Ur( " 2222 " , " ur " , " B " , 99 );
mongoOperation.save(urB);
//查找
Ur urB1 = mongoOperation.findOne(
new Query(Criteria.where( " id " ).is( " 2222 " )), Ur. class );
System.out.println(urB1);
//插⼊对象列表,放到arraylist中
System.out.println( " " );
Ur urC = new Ur( " 3333 " , " ur " , " C " , 99 );
Ur urD = new Ur( " 4444 " , " ur " , " D " , 99 );
Ur urE = new Ur( " 5555 " , " ur " , " E " , 99 );
List < Ur > urList = new ArrayList < Ur > ();
urList.add(urC);
urList.add(urD);
urList.add(urE);
mongoOperation.inrtList( " ABC-List " , urList);
List < Ur > urs = mongoOperation.find( " ABC-List " , new Query(Criteria
.where( " firstname " ).is( " ur " )), Ur. class );
for (Ur temp : urs) {
System.out.println(temp);
}
}
}
输出结果如下:
Ca 1 ...
Ur [id = 1111 , firstname = ur, lastname = A, age = 99 ]
动漫头像女生可爱Ca 2 ...
Ur [id = 2222 , firstname = ur, lastname = B, age = 99 ]
Ca 3 ...
Ur [id = 3333 , firstname = ur, lastname = C, age = 99 ]
Ur [id = 4444 , firstname = ur, lastname = D, age = 99 ]
Ur [id = 5555 , firstname = ur, lastname = E, age = 99 ]
更新Document
在mongodb中,可以使⽤save,updateFirst(),updateMulti()⽅法来进⾏更新,下⾯
是相关的例⼦
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringMongoConfig. class );
MongoOperations mongoOperation = (MongoOperations) ctx
.getBean( " mongoTemplate " );
Ur ur = new Ur( " 1000 " , " ur-first " , " ur-last " , 17 );
System.out.println( " by save() " );
mongoOperation.save(ur);
Ur urPrint1 = mongoOperation.findOne( new Query(Criteria.where( " id " ).is( " 1000 " )), Ur. class ); System.out.println(urPrint1);
//修改ur对象的lastname
ur.tLastname( " new last name " );
//更新ur对象
Ur urPrint2 = mongoOperation.findOne( new Query(Criteria.where( " id " )
.is( " 1000 " )), Ur. class );玫瑰什么时候开花
System.out.println(urPrint2);
//Ca 2 ... update firstname field, $t
System.out.println( " by updateFirst() - $t " );
//将id为1000的ur对象的firstname属性的值更新为”new firstname”
mongoOperation.updateFirst( " ur " ,
new Query(Criteria.where( " _id " ).is( " 1000 " )),
Update.update( " firstname " , " new first name " ));
Ur urPrint3 = mongoOperation.findOne( new Query(Criteria.where( " id " )
.is( " 1000 " )), Ur. class );
System.out.println(urPrint3);
//对id为1000的ur的age加上10
System.out.println( " by updateFirst() - $inc " );
Update updateAge = new Update();
updateAge.inc( " age " , 10 );
mongoOperation.updateFirst( " ur " ,
new Query(Criteria.where( " _id " ).is( " 1000 " )), updateAge);
Ur urPrint4 = mongoOperation.findOne( new Query(Criteria
.where( " _id " ).is( " 1000 " )), Ur. class );
System.out.println(urPrint4);
}
}
结果为:
Ca 1 ...by save()
Ur [id = 1000 , firstname = ur - first, lastname = ur - last, age = 17 ]
Ur [id = 1000 , firstname = ur - first, lastname = new last name, age = 17 ]
Ca 2 ...by updateFirst() - $t
Ur [id = 1000 , firstname = new first name, lastname = new last name, age = 17 ]
Ca 3 ...by updateFirst() - $inc
Ur [id = 1000 , firstname = new first name, lastname = new last name, age = 27 ]
此外,还⽀持使⽤updateMulti,updateMulti是将所有的对象进⾏更新,⽐如:
mongoOperation.updateMulti( " ur " ,
new Query(Criteria.where( " firstname " ).is( " yong " )),
Update.update( " age " , 40 ));
高腰裙
表⽰将所有firstname为yong的ur对象的age属性全部更新为40。
查询Document
在spring mongodb中,可以使⽤findOne(),find()和getCollection()去查询mongodb,常见的⽤法如下:
Ur ur = new Ur( " ... " );
//找到第⼀个id=1001的ur对象
Ur ur = mongoOperation.findOne( " test " , new Query(Criteria
.where( " id " ).is( " 1001 " )), Ur. class );
//从test集合中获得所有id<=1000并且age=21的ur对象
List < Ur > urs = mongoOperation.find( " test " , new Query(Criteria
.
where( " id " ).lte( " 2001 " ).and( " age " ).is( 21 )), Ur. class );
//从test 集合中获得所有的ur对象列表
List < Ur > urs = Collection( " test " , Ur. class );
认知力删除document
在spring mongodb中,删除document使⽤remove⽅法,⽰例如下:
在spring mongodb中,删除document使⽤remove⽅法,⽰例如下:
Ur ur = new Ur( " ... " );
//删除ur集合中的ur对象
//删除test集合下的id=2的ur对象欢乐颂2结局
.where( " id " ).is( " 2 " )));
/
/删除test集合下的,id=3的ur对象,最后并且返回这个被删除的对象Ur deletedUr = mongoOperation.findAndRemove( " test " , new Query(Criteria.where( " id " ).is( " 3 " )), Ur. class );