MongoDB 奇怪的 writeResult 行为
MongoDB weird writeResult behaviour
我正在使用 mongodb (v2.6.7) 和 mongo (2.6.7) shell 客户端。
我正在尝试使用插入和更新命令返回的 WriteResult 对象。
根据mongodocs,如果出现错误,它returns 一个带有 writeError 子文档的 writeResult 对象。但是我无法在 shell 或 mongo 的 javascript 文件中访问此子文档。
下面是我的问题的说明。
- 我插入一个对象并得到成功的writeResult。
- 然后我用相同的 _id 再次插入,并且我在正确设置 "writeError" 的屏幕上正确打印了 writeResult。
- 当我使用 printjson() 方法打印时,writeResult 对象似乎包含 writeError
- 但是当我用 JSON.stringify() 打印它时,我看不到 "writeError"。
- 另外 writeResult.writeError 未定义,所以我无法访问它的 writeResult.writeError.code 属性.
谁能解释一下为什么会这样,正确的方法是什么。
afv:PRIMARY>writeResult=db.sysinfo.insert({_id:"myid",otherData:"otherDataValue"})
WriteResult({ "nInserted" : 1 })
afv:PRIMARY>writeResult=db.sysinfo.insert({_id:"myid",otherData:"otherDataValue"})
写入结果({
"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.代码
2015-02-02T16:34:42.402+0530 类型错误:无法读取未定义的 属性 'code'
afv:PRIMARY> 写入结果["writeError"]
我不知道为什么要这样实现,但是要访问包含的 writeError
子文档,您可以在 WriteResult
对象上调用 getWriteError()
:
> 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
我找不到这方面的任何文档。我在 shell 中输入 writeResult.
后按两次 Tab 以查看可用内容。
除了使用已经提到的方法获取 WriteError 对象之外,您还可以获得原始响应并对其进行处理:
> writeResult.getRawResponse()
{
"writeErrors" : [
{
"index" : 0,
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }",
"op" : {
"_id" : ObjectId("54cf8152733aa5e886f0e400"),
"a" : 1
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}
所以,你可以做这样的事情,事情会更加一致:
> raw = writeResult.getRawResponse();
> raw.writeErrors[0].errmsg
insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }
> printjson(raw.writeErrors[0])
{
"index" : 0,
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }",
"op" : {
"_id" : ObjectId("54cf8152733aa5e886f0e400"),
"a" : 1
}
}
> JSON.stringify(raw.writeErrors[0])
{"code":11000,"index":0,"errmsg":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }"}
我正在使用 mongodb (v2.6.7) 和 mongo (2.6.7) shell 客户端。
我正在尝试使用插入和更新命令返回的 WriteResult 对象。
根据mongodocs,如果出现错误,它returns 一个带有 writeError 子文档的 writeResult 对象。但是我无法在 shell 或 mongo 的 javascript 文件中访问此子文档。
下面是我的问题的说明。
- 我插入一个对象并得到成功的writeResult。
- 然后我用相同的 _id 再次插入,并且我在正确设置 "writeError" 的屏幕上正确打印了 writeResult。
- 当我使用 printjson() 方法打印时,writeResult 对象似乎包含 writeError
- 但是当我用 JSON.stringify() 打印它时,我看不到 "writeError"。
- 另外 writeResult.writeError 未定义,所以我无法访问它的 writeResult.writeError.code 属性.
谁能解释一下为什么会这样,正确的方法是什么。
afv:PRIMARY>writeResult=db.sysinfo.insert({_id:"myid",otherData:"otherDataValue"}) WriteResult({ "nInserted" : 1 }) afv:PRIMARY>writeResult=db.sysinfo.insert({_id:"myid",otherData:"otherDataValue"}) 写入结果({ "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.代码 2015-02-02T16:34:42.402+0530 类型错误:无法读取未定义的 属性 'code' afv:PRIMARY> 写入结果["writeError"]
我不知道为什么要这样实现,但是要访问包含的 writeError
子文档,您可以在 WriteResult
对象上调用 getWriteError()
:
> 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
我找不到这方面的任何文档。我在 shell 中输入 writeResult.
后按两次 Tab 以查看可用内容。
除了使用已经提到的方法获取 WriteError 对象之外,您还可以获得原始响应并对其进行处理:
> writeResult.getRawResponse()
{
"writeErrors" : [
{
"index" : 0,
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }",
"op" : {
"_id" : ObjectId("54cf8152733aa5e886f0e400"),
"a" : 1
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}
所以,你可以做这样的事情,事情会更加一致:
> raw = writeResult.getRawResponse();
> raw.writeErrors[0].errmsg
insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }
> printjson(raw.writeErrors[0])
{
"index" : 0,
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }",
"op" : {
"_id" : ObjectId("54cf8152733aa5e886f0e400"),
"a" : 1
}
}
> JSON.stringify(raw.writeErrors[0])
{"code":11000,"index":0,"errmsg":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: foo.bar.$_id_ dup key: { : ObjectId('54cf8152733aa5e886f0e400') }"}