JAVA封装Mongodb操作之二:MongoOperations

mongoOperation相对于MongoRepository来说,方法更多一些,也更接近于mongo的原生态语言。它的方法包括了MongoRepository的所有方法外,还有分组查询group(),update方法,mapreduce方法以及直接操作collection的方法;好多都是方法的参数里都有query参数。


体类user如下:

package  com.mkyong.user;
public  class  User {
private  String id;
private  String firstname;
private  String lastname;
private  int  age;
//  getter and setter methods 
}

接下来,我们看具体的操作代码,如下,这里假设要将user类保存到名为userprofile的数据集中。

package  com.mkyong.core;

import  java.util.List;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.annotation.AnnotationConfigApplicationContext;
import  org.springframework.context.support.GenericXmlApplicationContext;
import  org.springframework.data.document.mongodb.MongoOperations;
import  org.springframework.data.document.mongodb.query.Criteria;
import  org.springframework.data.document.mongodb.query.Query;
import  org.springframework.data.document.mongodb.query.Update;
import  com.mkyong.config.SpringMongoConfig;
import  com.mkyong.user.User;
public  class  App
{
public  static  void  main( String[] args )
{
ApplicationContext ctx 
=  new  AnnotationConfigApplicationContext(SpringMongoConfig.  class  );
MongoOperations mongoOperation 
=  (MongoOperations)ctx.getBean(  "  mongoTemplate  "  );
User user 
=  new  User(  "  1001  "  "  yong  "  "  mook kim  "  30  );
//  保存 
mongoOperation.save(  "  userprofile  "  ,user);
//  查找 
User savedUser  =  mongoOperation.findOne(  "  userprofile  "  ,
new  Query(Criteria.where(  "  id  "  ).is(  "  1001  "  )),
User.
  class  );
System.out.println(
  "  savedUser :  "  +  savedUser);
//  更新 
mongoOperation.updateFirst(  "  userprofile  "  ,
new  Query(Criteria.where(  "  firstname  "  ).is(  "  yong  "  )), 
Update.update(
  "  lastname  "  "  new lastname  "  ));
User updatedUser 
=  mongoOperation.findOne(  "  userprofile  "  ,
new  Query(Criteria.where(  "  id  "  ).is(  "  1001  "  )),
User.
  class  );
System.out.println(
  "  updatedUser :  "  +  updatedUser);
//  删除 
mongoOperation.remove(  "  userprofile  "  ,
new  Query(Criteria.where(  "  id  "  ).is(  "  1001  "  )),
User.
  class  );
//  显示当前列表 
List  <  User  >  listUser  = 
mongoOperation.getCollection(
  "  userprofile  "  , User.  class  );
System.out.println(
  "  Number of user =  "  +  listUser.size());
}
}

 

输出结果如下:

savedUser : User [id  =  1001  , firstname  =  yong, lastname  =  mook kim, age  =  30  ]
updatedUser : User [id
  =  1001  , firstname  =  yong, lastname  =  new  lastname, age  =  30  ]
Number of user 
=

Spring mongodb插入数据

下面详细讲解如何使用spring mongodb插入数据。在spring mongodb中,插入数据到

mongodb有如下几种方法:

User user  =  new  User(  "  ...  "  );
//  将user对象保存到"user"这个collection中 
mongoOperation.save(user);
//  将user对象保存到"new collection"这个collection中 
mongoOperation.save(  "  new collection  "  ,user);
//  将user对象保存到"user"这个collection中 
mongoOperation.insert(user);
//  将user对象保存到"new collection"这个collection中 
mongoOperation.insert(
  "  new collection  "  , user);
//  将user的对象列表(List)保存到"user"collection中去 
mongoOperation.insertList(userInList);
//  将user的对象列表(List)保存到"new collection"collection中去 
mongoOperation.insertList(  "  new collection  "  , userInList);

要注意的是,Spring mongodb中,当没有指定collection时,就会把对象保存到以对象命名的collection中。比如上例中的mongoOperation.insert(user),由于没指定collection的名称,所以会把user对象保存到user这个新建立的collection中。

另外请注意其中的save和insert的区别。它们的区别为:

1)save意思是,当记录不存在时插入,或者是当记录已存在是更新,实际上就是saveorupdate的意思。

2) insert的意思是:当记录不存在时插入,而如果记录存在时则忽略,继续插入。

下面举例子说明:

package  com.mkyong.core;
import  java.util.ArrayList;
import  java.util.List;
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.annotation.AnnotationConfigApplicationContext;
import  org.springframework.data.document.mongodb.MongoOperations;
import  org.springframework.data.document.mongodb.query.Criteria;
import  org.springframework.data.document.mongodb.query.Query;
import  com.mkyong.config.SpringMongoConfig;
import  com.mkyong.user.User;
public  class  App {
public  static  void  main(String[] args) {
ApplicationContext ctx 
=  new  AnnotationConfigApplicationContext(
SpringMongoConfig.
  class  );
MongoOperations mongoOperation 
=  (MongoOperations) ctx
.getBean(
  "  mongoTemplate  "  );
//  新增一个user对象,并把它放到"ABC"这个collection中 
System.out.println(  "  Case 1...  "  );
User userA 
=  new  User(  "  1111  "  "  user  "  "  A  "  99  );
mongoOperation.save(
  "  ABC  "  , userA);
//  查找刚插入的user对象 
User userA1  =  mongoOperation.findOne(  "  ABC  "  ,
new  Query(Criteria.where(  "  id  "  ).is(  "  1111  "  )), User.  class  );
System.out.println(userA1);
//  插入新的user,放到userB这个collection中去 
System.out.println(  "  Case 2...  "  );
User userB 
=  new  User(  "  2222  "  "  user  "  "  B  "  99  );
mongoOperation.save(userB);
//  查找 
User userB1  =  mongoOperation.findOne(
new  Query(Criteria.where(  "  id  "  ).is(  "  2222  "  )), User.  class  );
System.out.println(userB1);
//  插入对象列表,放到arraylist中 
System.out.println(  "  Case 3...  "  );
User userC 
=  new  User(  "  3333  "  "  user  "  "  C  "  99  );
User userD 
=  new  User(  "  4444  "  "  user  "  "  D  "  99  );
User userE 
=  new  User(  "  5555  "  "  user  "  "  E  "  99  );
List
  <  User  >  userList  =  new  ArrayList  <  User  >  ();
userList.add(userC);
userList.add(userD);
userList.add(userE);
mongoOperation.insertList(
  "  ABC-List  "  , userList);
List
  <  User  >  users  =  mongoOperation.find(  "  ABC-List  "  new  Query(Criteria
.where(
  "  firstname  "  ).is(  "  user  "  )), User.  class  );
for  (User temp : users) {
System.out.println(temp);
}
}
}

 

输出结果如下:

Case  1  ...
User [id
  =  1111  , firstname  =  user, lastname  =  A, age  =  99  ]
Case 
2  ...
User [id
  =  2222  , firstname  =  user, lastname  =  B, age  =  99  ]
Case 
3  ...
User [id
  =  3333  , firstname  =  user, lastname  =  C, age  =  99  ]
User [id
  =  4444  , firstname  =  user, lastname  =  D, age  =  99  ]
User [id
  =  5555  , firstname  =  user, 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  "  );
User user 
=  new  User(  "  1000  "  "  user-first  "  "  user-last  "  17  );
System.out.println(
  "  Case 1...by save()  "  );
mongoOperation.save(user);
User userPrint1 
=  mongoOperation.findOne(  new  Query(Criteria.where(  "  id  "  ).is(  "  1000  "  )), User.  class  );
System.out.println(userPrint1);
//  修改user对象的lastname 
user.setLastname(  "  new last name  "  );
//  更新user对象 
mongoOperation.save(user);
User userPrint2 
=  mongoOperation.findOne(  new  Query(Criteria.where(  "  id  "  )
.is(
  "  1000  "  )), User.  class  );
System.out.println(userPrint2);
//  Case 2 ... update firstname field, $set 
System.out.println(  "  Case 2...by updateFirst() - $set  "  );
//  将id为1000的user对象的firstname属性的值更新为”new firstname” 
mongoOperation.updateFirst(  "  user  "  ,
new  Query(Criteria.where(  "  _id  "  ).is(  "  1000  "  )),
Update.update(
  "  firstname  "  "  new first name  "  ));
User userPrint3 
=  mongoOperation.findOne(  new  Query(Criteria.where(  "  id  "  )
.is(
  "  1000  "  )), User.  class  );
System.out.println(userPrint3);
//  对id为1000的user的age加上10 
System.out.println(  "  Case 3...by updateFirst() - $inc  "  );
Update updateAge 
=  new  Update();
updateAge.inc(
  "  age  "  10  );
mongoOperation.updateFirst(
  "  user  "  ,
new  Query(Criteria.where(  "  _id  "  ).is(  "  1000  "  )), updateAge);
User userPrint4 
=  mongoOperation.findOne(  new  Query(Criteria
.where(
  "  _id  "  ).is(  "  1000  "  )), User.  class  );
System.out.println(userPrint4);
}
}

结果为:

Case  1  ...by save()
User [id
  =  1000  , firstname  =  user  -  first, lastname  =  user  -  last, age  =  17  ]
User [id
  =  1000  , firstname  =  user  -  first, lastname  =  new  last name, age  =  17  ]
Case 
2  ...by updateFirst()  -  $set
User [id
  =  1000  , firstname  =  new  first name, lastname  =  new  last name, age  =  17  ]
Case 
3  ...by updateFirst()  -  $inc
User [id
  =  1000  , firstname  =  new  first name, lastname  =  new  last name, age  =  27  ]

 

此外,还支持使用updateMulti,updateMulti是将所有的对象进行更新,比如:

mongoOperation.updateMulti(  "  user  "  ,
new  Query(Criteria.where(  "  firstname  "  ).is(  "  yong  "  )),
Update.update(
  "  age  "  40  ));

表示将所有firstname为yong的user对象的age属性全部更新为40。

查询Document

在spring mongodb中,可以使用findOne(),find()和getCollection()去查询mongodb,常见的用法如下:

User user  =  new  User(  "  ...  "  );
//  找到第一个id=1001的user对象 
User user  =  mongoOperation.findOne(  "  test  "  new  Query(Criteria
.where(
  "  id  "  ).is(  "  1001  "  )), User.  class  );
//  从test集合中获得所有id<=1000并且age=21的user对象 
List  <  User  >  users  =  mongoOperation.find(  "  test  "  new  Query(Criteria
.where(
  "  id  "  ).lte(  "  2001  "  ).and(  "  age  "  ).is(  21  )), User.  class  );
//  从test 集合中获得所有的user对象列表 
List  <  User  >  users  =  mongoOperation.getCollection(  "  test  "  , User.  class  );

删除document

在spring mongodb中, 删除document使用remove方法,示例如下:

在spring mongodb中, 删除document使用remove方法,示例如下:
User user 
=  new  User(  "  ...  "  );
//  删除user集合中的user对象 
mongoOperation.remove(user);
//  删除test集合下的id=2的user对象 
mongoOperation.remove(  "  test  "  new  Query(Criteria
.where(
  "  id  "  ).is(  "  2  "  )));
//  删除test集合下的,id=3的user对象,最后并且返回这个被删除的对象 
User deletedUser  =  mongoOperation.findAndRemove(  "  test  "  ,
new  Query(Criteria.where(  "  id  "  ).is(  "  3  "  )), User.  class  );



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值