Search code examples
mongodbmongodb-shell

does MongoDB's getLastError() command indicate the number of records affected by a $rename command?


I'm developing an application that preens imported data using field maps in a "dictionary" collection, and am trying to see how many records were affected by a $rename operation. When does the mongo shell call db.getLastError() on behalf of the user? I've read that it does call getLastError() before accepting new statements interactively in the console, but what are the semantics for command-line invocations like mongo script.js? I'm assuming getLastError() is not called at all for scripts, but haven't been able to find confirmation in the documentation.


Solution

  • From the Opening New Connections doc:

    ... Additionally, inside the script, you would need to call db.getLastErrorObj() or db.getLastError() explicitly to wait for the result of write operations.

    The db.getLastErrorObj() is what you want to call to get the result of the update/$rename operation. It returns and object that looks like:

    {
        "updatedExisting" : true,
        "n" : 2,
        "connectionId" : 35,
        "err" : null,
        "ok" : 1
    }
    

    n is the number of updated documents.