mongo WriteResult

来源:互联网 发布:40岁程序员干不了 编辑:程序博客网 时间:2024/06/04 18:50

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
原创粉丝点击