mongo WriteResult

WriteResult 反编译源码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.mongodb;

import com.mongodb.MongoException.Network;
import java.io.IOException;

public class WriteResult {
    private long _lastCall;
    private WriteConcern _lastConcern;
    private CommandResult _lastErrorResult;
    private final DB _db;
    private final DBPort _port;
    private final boolean _lazy;

    WriteResult(CommandResult o, WriteConcern concern) {
        this._lastErrorResult = o;
        this._lastConcern = concern;
        this._lazy = false;
        this._port = null;
        this._db = null;
    }

    WriteResult(DB db, DBPort p, WriteConcern concern) {
        this._db = db;
        this._port = p;
        this._lastCall = p.getUsageCount();
        this._lastConcern = concern;
        this._lazy = true;
    }

    /** @deprecated */
    @Deprecated
    public CommandResult getCachedLastError() {
        return this._lastErrorResult;
    }

    /** @deprecated */
    @Deprecated
    public WriteConcern getLastConcern() {
        return this._lastConcern;
    }

    /** @deprecated */
    @Deprecated
    public synchronized CommandResult getLastError() {
        return this.getLastError((WriteConcern)null);
    }

    /** @deprecated */
    @Deprecated
    public synchronized CommandResult getLastError(WriteConcern concern) {
        if(this._lastErrorResult == null || concern != null && (this._lastConcern == null || this._lastConcern.getW() < concern.getW())) {
            if(this._port != null) {
                try {
                    this._lastErrorResult = this._port.tryGetLastError(this._db, this._lastCall, concern == null?new WriteConcern():concern);
                } catch (IOException var3) {
                    throw new Network(var3.getMessage(), var3);
                }

                if(this._lastErrorResult == null) {
                    throw new IllegalStateException("The connection may have been used since this write, cannot obtain a result");
                } else {
                    this._lastConcern = concern;
                    ++this._lastCall;
                    return this._lastErrorResult;
                }
            } else {
                throw new IllegalStateException("Don't have a port to obtain a write result, and existing one is not good enough.");
            }
        } else {
            return this._lastErrorResult;
        }
    }

    /** @deprecated */
    @Deprecated
    public String getError() {
        Object foo = this.getField("err");
        return foo == null?null:foo.toString();
    }

    public int getN() {
        return this.getLastError().getInt("n");
    }

    public Object getUpsertedId() {
        return this.getLastError().get("upserted");
    }

    public boolean isUpdateOfExisting() {
        return this.getLastError().getBoolean("updatedExisting");
    }

    /** @deprecated */
    @Deprecated
    public Object getField(String name) {
        return this.getLastError().get(name);
    }

    /** @deprecated */
    @Deprecated
    public boolean isLazy() {
        return this._lazy;
    }

    public String toString() {
        CommandResult res = this.getCachedLastError();
        return res != null?res.toString():"N/A";
    }
}

getN()方法

getLastError.n reports the number of documents updated or removed
只计算updated和removed的记录;
insert的记录不列入其中;

测试

测试方法:db.getLastErrorObj() 获取最后一次操作结果信息

insert 测试:
insert 方法 ,db.getLastErrorObj().n==0
insert 方法 ,db.getLastErrorObj().n==0

update测试:
update 方法,db.getLastErrorObj().n==3
update 方法,db.getLastErrorObj().n==3

remove测试:
db.getLastErrorObj().n==1
remove 方法 ,db.getLastErrorObj().n==1

mongo文档信息

https://docs.mongodb.com/manual/reference/write-concern/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值