template操作mongodb数据库(更新方法大全)

本文是使用JAVA程序操作MongoDB数据库。里面提供了各种更新数据的方法,查询的各种方法会在后面进行更新。
本文只是提供了数据库更新操作的一些方法。
数据库数据和字段如下:
在这里插入图片描述
在这里插入图片描述
对于更新数据,我将更新数据的方法抽象出来成为单独一个方法,方法如下:

private int execUpdate(Query query, Update update,boolean isMany)
    {
        if (isMany)
        {
            WriteResult result = template.updateMulti(query, update, Blog.class);
            return result.getN();
        }

            WriteResult result = template.updateFirst(query, update, Blog.class);
            return result.getN();

    }

该方法可以更新一行或者多行的数据。
1.插入数据

    public int  insertBlog(Blog blog) {
        template.insert(blog);
        return 1;
    }

2.根据某列更新某列内容;

public int updateContentsForId(String id, String contents) {
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();
        update.set("contents",contents);
        return this.execUpdate(query,update,false);

    }

3.添加一列,并给出那列的默认值。

public int addBstatus(String keyName, String defaultValue) {
        Update update=new Update();
        update.set(keyName,defaultValue);
        return this.execUpdate(null,update,true);
    }

4.给某个数组添加内容

public int addKeyword(String title, String keyword) {
        Query query=new Query();
        query.addCriteria(Criteria.where("title").is(title));
        Update update=new Update();
        update.addToSet("keywords",keyword);
        return this.execUpdate(query,update,false);
    }

5.添加某个嵌套子文档:

 public int addPdetail(String title, Pdetail pdetail) {
        Query query=new Query();
        query.addCriteria(Criteria.where("title").is(title));
        Update update=new Update();
        update.push("pdetails",pdetail);
        update.inc("pcount",1);
        return this.execUpdate(query,update,false);
    }

6.更新嵌套子文档中的内容

    public int updatePl(String title, String plid, String plcontent, Date pltime) {
        Query query=new Query();
        query.addCriteria(Criteria.where("title").is(title).and("pdetails.plid").is("2"));
        Update update=new Update();
        update.set("pdetails.$.plcontent",plcontent);
        update.set("pdetails.$.pltime",pltime);
        return this.execUpdate(query,update,false);
    }

7.删除数组中的第一行

    public int deleteFirstPl(String title) {
        Query query=new Query();
        query.addCriteria(Criteria.where("title").is(title));
        Update update=new Update();
        update.pop("pdetails",Update.Position.FIRST);
        return this.execUpdate(query,update,false);

    }

8.删除数组中的最后一行

 public int deleteLastPl(String title) {
        Query query=new Query();
        query.addCriteria(Criteria.where("title").is(title));
        Update update=new Update();
        update.pop("pdetails",Update.Position.LAST);
        return this.execUpdate(query,update,false);
    }

9.删除某个嵌套子文档的全部内容。

public int deleteAllPlAndClosePl(String title) {
        Query query=new Query();
        query.addCriteria(Criteria.where("title").is(title));
        Update update=new Update();
        Pdetail pdetail=new Pdetail();
        update.pull("pdetails",pdetail);//方法一
//        update.unset("pdetails");//方法二
        update.set("pstate",0);
        return this.execUpdate(query,update,true);
    }

10.删除集合

public void dropCollection() {
        template.dropCollection(Blog.class);

    }

11.删除数据库

public void dropDateBase() {
        template.getDb().dropDatabase();
    }

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值