From 1a940972e9e1ed78adfff6f31c2907a2c860d17c Mon Sep 17 00:00:00 2001 From: Tess Avitabile Date: Mon, 10 Oct 2016 13:18:25 -0400 Subject: SERVER-26513 listCollections output should include _id index spec as idIndex --- jstests/core/collation.js | 10 +++++-- jstests/core/list_collections1.js | 34 ++++++++++++++++++++++ .../multiVersion/libs/verify_collection_data.js | 19 ++++++++++-- src/mongo/db/commands/list_collections.cpp | 6 ++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/jstests/core/collation.js b/jstests/core/collation.js index 7b8a939a042..301ad6195b6 100644 --- a/jstests/core/collation.js +++ b/jstests/core/collation.js @@ -1953,7 +1953,7 @@ // Create a collection with a non-simple default collation. assert.commandWorked( sourceDB.runCommand({create: coll.getName(), collation: {locale: "en", strength: 2}})); - const sourceCollectionInfos = sourceDB.getCollectionInfos({name: coll.getName()}); + var sourceCollectionInfos = sourceDB.getCollectionInfos({name: coll.getName()}); assert.writeOK(sourceDB[coll.getName()].insert({_id: "FOO"})); assert.writeOK(sourceDB[coll.getName()].insert({_id: "bar"})); @@ -1963,7 +1963,13 @@ assert.commandWorked( sourceDB.adminCommand({copydb: 1, fromdb: sourceDB.getName(), todb: destDB.getName()})); - const destCollectionInfos = destDB.getCollectionInfos({name: coll.getName()}); + var destCollectionInfos = destDB.getCollectionInfos({name: coll.getName()}); + + // The namespace for the _id index will differ since the source and destination collections + // are in different databases. + delete sourceCollectionInfos[0].idIndex.ns; + delete destCollectionInfos[0].idIndex.ns; + assert.eq(sourceCollectionInfos, destCollectionInfos); assert.eq([{_id: "FOO"}], destDB[coll.getName()].find({_id: "foo"}).toArray()); } diff --git a/jstests/core/list_collections1.js b/jstests/core/list_collections1.js index 1ccca1ba680..c8c3f92fbc9 100644 --- a/jstests/core/list_collections1.js +++ b/jstests/core/list_collections1.js @@ -32,6 +32,40 @@ assert.eq('object', typeof(collObj.options)); assert.eq('collection', collObj.type, tojson(collObj)); assert.eq(false, collObj.info.readOnly, tojson(collObj)); + assert.eq("object", typeof(collObj.idIndex), tojson(collObj)); + assert(collObj.idIndex.hasOwnProperty("v"), tojson(collObj)); + + // + // Test basic command output for views. + // + + assert.commandWorked(mydb.createView("bar", "foo", [])); + res = mydb.runCommand("listCollections"); + assert.commandWorked(res); + collObj = res.cursor.firstBatch.filter(function(c) { + return c.name === "bar"; + })[0]; + assert(collObj); + assert.eq("object", typeof(collObj.options), tojson(collObj)); + assert.eq("foo", collObj.options.viewOn, tojson(collObj)); + assert.eq([], collObj.options.pipeline, tojson(collObj)); + assert.eq("view", collObj.type, tojson(collObj)); + assert.eq(true, collObj.info.readOnly, tojson(collObj)); + assert(!collObj.hasOwnProperty("idIndex"), tojson(collObj)); + + // + // Test basic command output for system.indexes. + // + + collObj = res.cursor.firstBatch.filter(function(c) { + return c.name === "system.indexes"; + })[0]; + if (collObj) { + assert.eq("object", typeof(collObj.options), tojson(collObj)); + assert.eq("collection", collObj.type, tojson(collObj)); + assert.eq(false, collObj.info.readOnly, tojson(collObj)); + assert(!collObj.hasOwnProperty("idIndex"), tojson(collObj)); + } // // Test basic usage with DBCommandCursor. diff --git a/jstests/multiVersion/libs/verify_collection_data.js b/jstests/multiVersion/libs/verify_collection_data.js index eb7cb952286..ab96a1ef29c 100644 --- a/jstests/multiVersion/libs/verify_collection_data.js +++ b/jstests/multiVersion/libs/verify_collection_data.js @@ -65,7 +65,7 @@ createCollectionWithData = function(db, collectionName, dataGenerator) { // MongoDB 3.4 introduces new fields into the listCollections result document. This function // injects expected 3.4 values into a 3.2 document to allow for object comparison. -// TODO: Remove this check post-3.4 release. +// TODO SERVER-26676: Remove this check post-3.4 release. var injectExpected34FieldsIf32 = function(collectionInfo, dbVersion) { if (dbVersion.startsWith("3.2")) { return Object.extend({type: "collection", info: {readOnly: false}}, collectionInfo); @@ -74,6 +74,18 @@ var injectExpected34FieldsIf32 = function(collectionInfo, dbVersion) { return collectionInfo; }; +// MongoDB 3.4 introduces a new field 'idIndex' into the listCollections result document. This +// cannot be injected into the 3.2 listCollections result document because the full index spec is +// not known. This function removes the 'idIndex' field to allow for object comparison. +// TODO SERVER-26676: Remove this check post-3.4 release. +var removeIdIndexField = function(collectionInfo) { + if (collectionInfo.hasOwnProperty("idIndex")) { + delete collectionInfo.idIndex; + } + + return collectionInfo; +}; + // Class to save the state of a collection and later compare the current state of a collection to // the saved state function CollectionDataValidator() { @@ -127,8 +139,9 @@ function CollectionDataValidator() { // Get the metadata for this collection var newCollectionInfo = this.getCollectionInfo(collection); - let colInfo1 = injectExpected34FieldsIf32(_collectionInfo, _dbVersion); - let colInfo2 = injectExpected34FieldsIf32(newCollectionInfo, dbVersionForCollection); + let colInfo1 = removeIdIndexField(injectExpected34FieldsIf32(_collectionInfo, _dbVersion)); + let colInfo2 = removeIdIndexField( + injectExpected34FieldsIf32(newCollectionInfo, dbVersionForCollection)); assert.docEq(colInfo1, colInfo2, "collection metadata not equal"); // Get the indexes for this collection diff --git a/src/mongo/db/commands/list_collections.cpp b/src/mongo/db/commands/list_collections.cpp index 0590ad108ff..114d98faf55 100644 --- a/src/mongo/db/commands/list_collections.cpp +++ b/src/mongo/db/commands/list_collections.cpp @@ -46,6 +46,7 @@ #include "mongo/db/db_raii.h" #include "mongo/db/exec/queued_data_stage.h" #include "mongo/db/exec/working_set.h" +#include "mongo/db/index/index_descriptor.h" #include "mongo/db/matcher/extensions_callback_disallow_extensions.h" #include "mongo/db/query/cursor_request.h" #include "mongo/db/query/cursor_response.h" @@ -167,6 +168,11 @@ BSONObj buildCollectionBson(OperationContext* txn, const Collection* collection) BSONObj info = BSON("readOnly" << storageGlobalParams.readOnly); b.append("info", info); + auto idIndex = collection->getIndexCatalog()->findIdIndex(txn); + if (idIndex) { + b.append("idIndex", idIndex->infoObj()); + } + return b.obj(); } -- cgit v1.2.1