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();
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章