JAVA程序(MongoTemplate)操作mongodb数据库常用方法(超级详细)

这里使用的是Spring+MongoTemplate来操作mongodb数据库。如果有不了解spring的同志先去了解一下spring为好,这里给出实现的一些方法。
主要有:
查询
增加
修改
删除
多字段增加
模糊查询
嵌套子文档修改。


/**
 * Created by hongyonghan
 */
@Component("studentDaoImpl")
public class StudentDaoImpl implements StudentDao {
    @Autowired
    private MongoTemplate template;
    @Override
    public String findStudentEmailForStuName(String stuname) {
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").is(stuname));
        Student student = template.find(query, Student.class).get(0);
        return student.getEmail();
    }

    @Override
    public int addStudent(Student student) {
        template.insert(student);
        return 1;
    }

    @Override
    public int addAgeForStuName(String stuname) {
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").is(stuname));
        Update update=new Update();
        update.inc("age",2);
        return this.execUpdate(query,update,false);
    }

    @Override
    public int updateAgeForStuAge(int gtAge, int ltAge, int newAge) {
        Query query=new Query();
        query.addCriteria(Criteria.where("age").lt(ltAge).gt(gtAge));
        Update update=new Update();
        update.set("age",newAge);
        return this.execUpdate(query,update,true);
    }

    @Override
    public int UpdateBookForStuName(String stuname, String updateBook) {
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").is(stuname));
        Update update=new Update();
        update.addToSet("books",updateBook);
        return this.execUpdate(query,update,false);

    }

    @Override
    public int updateBooksArrayForStuName(String stuname, String[] books) {
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").is(stuname));
        Update update=new Update();
        update.addToSet("books").each(books);
        return this.execUpdate(query,update,false);
    }

    @Override
    public int updateHadoopAndHiveForStuName(String stuname, int newHadoop, int newHive) {
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").is(stuname));
        Update update=new Update();
        update.set("hadoop",newHadoop);
        update.set("hive",newHive);
        return this.execUpdate(query,update,false);
    }

    @Override
    public int updateSchoolForStuName(String stuname, School school) {
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").is(stuname));
        Update update=new Update();
        update.push("schools",school);
        return this.execUpdate(query,update,false);
    }

    @Override
    public List<Student> findStudentPattern(String pattern_name) {
        Pattern pattern=Pattern.compile("^.*"+pattern_name+".*$",Pattern.CASE_INSENSITIVE);
        Query query=new Query();
        query.addCriteria(Criteria.where("stuname").regex(pattern));
        List<Student> list = template.find(query, Student.class);
        return list;
    }

    @Override
    public int updateSchoolValueForStuNameAndSchoolName(String stuname, String schoolname,String newScore) {
        Query query=new Query(Criteria.where("stuname").is(stuname).and("schools.schoolname").is(schoolname));
        Update update=new Update();
        update.set("schools.$.score",newScore);
        return this.execUpdate(query,update,false);
    }

    //将更新抽象出一个方法
    public int execUpdate(Query query,Update update,boolean isMany)
    {
        WriteResult result=null;
        if(isMany)
        {
            result=template.updateMulti(query, update, Student.class);
        }
        else
        {
            result=template.updateFirst(query, update, Student.class);
        }

        return result.getN();
    }

}

你可能感兴趣的:(mongodb)