Nitiraj
Nitiraj

Reputation: 644

MongoDB weird writeResult behaviour

I am using mongodb (v2.6.7) and mongo (2.6.7) shell client.

I am trying to use the WriteResult object returned by the insert and and update commands.

According to mongodocs in case of error it returns a writeResult object with writeError subdocument. But I am not able to access this subdocument in shell or in mongo's javascript file.

Below is an illustration of my problem.

Can someone please explain why is this happening and what is the correct way.


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"]

Upvotes: 4

Views: 2106

Answers (2)

Adam Comerford
Adam Comerford

Reputation: 21682

In addition to using the methods to get the WriteError object as mentioned already, you can also get the raw response and work on that:

> 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" : [ ]
}

So, you can then do something like this and things are a bit more consistent:

> 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') }"}

Upvotes: 0

JohnnyHK
JohnnyHK

Reputation: 311875

I don't know why it was implemented this way, but to access the contained writeError subdocument, you call getWriteError() on a WriteResult object:

> 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

I couldn't find any documentation for this. I figured it out by hitting Tab twice after typing writeResult. in the shell to see what's available.

Upvotes: 8

Related Questions