[SERVER-7168] Don't cache ensureIndex in the shell Created: 26/Sep/12  Updated: 11/Jul/16  Resolved: 12/Nov/12

Status: Closed
Project: Core Server
Component/s: JavaScript, Shell
Affects Version/s: None
Fix Version/s: 2.3.1

Type: Bug Priority: Major - P3
Reporter: Mathias Stearn Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-15693 DBClientWithCommands should not cache... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

It should test getLastError == null rather than ""

> db.asdf.ensureIndex
function (keys, options) {
    var name = this._indexSpec(keys, options).name;
    this._indexCache = this._indexCache || {};
    if (this._indexCache[name]) {
        return;
    }
    this.createIndex(keys, options);
    if (this.getDB().getLastError() == "") {
        this._indexCache[name] = true;
    }
}
 
> db.asdf.ensureIndex({a:1}); db.getLastError()
null
> db.asdf.ensureIndex({a:1}); db.getLastError() == ""
false

Note that if we fix this we need to make sure that the _indexCache does not persist across calls to db.eval() as the cache won't be invalidated at the necessary times.

Also, it may be best to just not cache ensureIndex in the shell. If someone runs a script that drops an index, they would expect ensureIndex in an admin shell to rebuild it, even if the shell had been open a while.



 Comments   
Comment by Sam Kleinman (Inactive) [ 19/Mar/13 ]

this was never documented, so this fix doesn't require a documentation change.

Comment by Mathias Stearn [ 12/Nov/12 ]

Not backward breaking because old code didn't cache correctly.

Comment by auto [ 12/Nov/12 ]

Author:

{u'date': u'2012-11-12T19:08:58Z', u'email': u'mathias@10gen.com', u'name': u'Mathias Stearn'}

Message: SERVER-7168 Don't cache ensureIndex in shell
Branch: master
https://github.com/mongodb/mongo/commit/7f9749e7293d1891497d69744d6dedd1f1e81798

Comment by Mathias Stearn [ 12/Nov/12 ]

Rename to reflect intended change. Original title: "ensureIndex caching is broken in JS"

Generated at Thu Feb 08 03:13:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.