Nit*_*raj 4 mongodb mongo-shell
我正在使用mongodb(v2.6.7)和mongo(2.6.7)shell客户端.
我试图使用insert和update命令返回的WriteResult对象.
根据mongodocs的错误,它返回一个带有writeError子文档的writeResult对象.但我无法在shell或mongo的javascript文件中访问此子文档.
以下是我的问题的说明.
有人可以解释为什么会发生这种情况,以及正确的方法是什么.
afv:PRIMARY>writeResult=db.sysinfo.insert({_id:"myid",otherData:"otherDataValue"})
WriteResult({ "nInserted" : 1 })
afv:PRIMARY>writeResult=db.sysinfo.insert({_id:"myid",otherData:"otherDataValue"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: afvadmin.sysinfo.$_id_ dup key: { : \"myid\" }"
}
})
afv:PRIMARY> printjson(writeResult)
{
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: afvadmin.sysinfo.$_id_ dup key: { : \"myid\" }"
}
}
afv:PRIMARY> JSON.stringify(writeResult);
{"nInserted":0,"nUpserted":0,"nMatched":0,"nModified":0,"nRemoved":0}
afv:PRIMARY> writeResult.writeError
afv:PRIMARY> writeResult.nInserted
0
afv:PRIMARY> writeResult.writeError.code
2015-02-02T16:34:42.402+0530 TypeError: Cannot read property 'code' of undefined
afv:PRIMARY> writeResult["writeError"]
我不知道为什么它以这种方式实现,但是为了访问包含的writeError子文档,你调用getWriteError()一个WriteResult对象:
> writeResult.getWriteError()
WriteError({
"index": 0,
"code": 11000,
"errmsg": "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_ dup key: { : \"myid\" }",
"op": {
"_id": "myid",
"otherData": "otherDataValue"
}
})
> writeResult.getWriteError().code
11000
Run Code Online (Sandbox Code Playgroud)
我找不到任何相关的文档.我在打开shell Tab后点击两次writeResult.以查看可用内容,从而弄明白了.