DB Script To Create Indexes In Mongo

From Znode Knowledge Base
Jump to: navigation, search
Introduction:

A provision to create indexes in MongoDB is made to enhance the performance of the Mongo DB.


Mongo Command- db.collection.createIndex()

The db.collection.createIndex() method is used to build an index on a collection.

The syntax for this command is as follows: db.collection.createIndex(keys, options)

Script Details:

A. db.categoryentity.createIndex({DisplayOrder: 1, ZnodeCategoryId: 1, ZnodeCatalogId: 1 , LocaleId: 1, VersionId: 1 , IsActive: 1})

Category Entity

1. Indexes needed

  • DisplayOrder
  • ZnodeCategoryId
  • ZnodeCatalogId
  • LocaleId
  • VersionId
  • IsActive

2. Order By

  • DisplayOrder

Our Recommendation of queries to create the indexes:-

db.getCollection("categoryentity").createIndex( { "DisplayOrder": 1 } )
db.getCollection("categoryentity").createIndex( { "ZnodeCategoryId": 1 } )
db.getCollection("categoryentity").createIndex( { "ZnodeCatalogId": 1 } )
db.getCollection("categoryentity").createIndex( { "LocaleId": 1 } )
db.getCollection("categoryentity").createIndex( { "VersionId": 1 } )
db.getCollection("categoryentity").createIndex( { "IsActive": 1 } )

B. db.logmessageentity.createIndex({CreatedDate: 1})

LogMessageEntity

a. Indexes needed

  • CreatedDate

b. Order By

  • CreatedDate

Our Recommendation of queries to create the indexes:-

db.getCollection("logmessageentity").createIndex( { "CreatedDate": 1 } )

C. db.versionentity.createIndex({ ZnodeCatalogId: 1, RevisionType: 1, LocaleId: 1})

VersionEntity

a. Indexes needed

  • ZnodeCatalogId
  • RevisionType
  • LocaleId
Our Recommendation of queries to create the indexes:-

db.getCollection("versionentity").createIndex( { "ZnodeCatalogId": 1 } )
db.getCollection("versionentity").createIndex( { "RevisionType": 1 } )
db.getCollection("versionentity").createIndex( { "LocaleId": 1 } )

D. db.configurableproductentity.createIndex({ZnodeProductId: 1, VersionId: 1})

ConfigurableProductEntity

a. Indexes needed

  • ZnodeProductId
  • VersionId

Our Recommendation of queries to create the indexes:-

db.getCollection("configurableproductentity").createIndex( { "ZnodeProductId": 1 } )
db.getCollection("configurableproductentity").createIndex( { "VersionId": 1 } )

E. db.seoentitiy.createIndex({PortalId: 1, SEOTypeName: 1, LocaleId: 1 , VersionId: 1, SEOCode: 1})

SEOEntity

a. Indexes needed

  • PortalId
  • SEOTypeName
  • LocaleId
  • VersionId
  • SEOCode

Our Recommendation of queries to create the indexes:-

db.getCollection("seoentity").createIndex( { "PortalId": 1 } )
db.getCollection("seoentity").createIndex( { "SEOTypeName": 1 } )
db.getCollection("seoentity").createIndex( { "LocaleId": 1 } )
db.getCollection("seoentity").createIndex( { "VersionId": 1 } )
db.getCollection("seoentity").createIndex( { "SEOCode": 1 } )

F. db.addonentity.createIndex({DisplayOrder: 1, ZnodeProductId: 1, LocaleId: 1 , VersionId: 1, RequiredType: 1})

AddonEntity

a. Indexes needed

  • DisplayOrder
  • ZnodeProductId
  • LocaleId
  • VersionId
  • RequiredType

b. Order By

  • DisplayOrder

Our Recommendation of queries to create the indexes:-

db.getCollection("addonentity").createIndex( { "DisplayOrder": 1 } )
db.getCollection("addonentity").createIndex( { "ZnodeProductId": 1 } )
db.getCollection("addonentity").createIndex( { "LocaleId": 1 } )
db.getCollection("addonentity").createIndex( { "VersionId": 1 } )
db.getCollection("addonentity").createIndex( { "RequiredType": 1 } )

G. db.productentity.createIndex({ZnodeProductId: 1, SKULower: 1, ZnodeCatalogId: 1 , ZnodeCategoryIds: 1, LocaleId: 1, IsActive: 1 , VersionId: 1, ProductIndex: 1})

ProductEntity

a. Indexes needed

  • ZnodeProductId
  • SKULower
  • ZnodeCatalogId
  • ZnodeCategoryIds
  • LocaleId
  • IsActive
  • VersionId
  • ProductIndex

Our Recommendation of queries to create the indexes:-

db.getCollection("productentity").createIndex( { "ZnodeProductId": 1 } )
db.getCollection("productentity").createIndex( { "SKULower": 1 } )
db.getCollection("productentity").createIndex( { "ZnodeCatalogId": 1 } )
db.getCollection("productentity").createIndex( { "ZnodeCategoryIds": 1 } )
db.getCollection("productentity").createIndex( { "LocaleId": 1 } )
db.getCollection("productentity").createIndex( { "ProductIndex": 1 } )
db.getCollection("productentity").createIndex( { "IsActive": 1 } )
db.getCollection("productentity").createIndex( { "VersionId": 1 } )

H. db.contentpageconfigentity.createIndex({PortalId: 1, IsActive: 1, ProfileId: 1 , VersionId: 1})

ContentPageConfigEntity

a. Indexes needed

  • PortalId
  • IsActive
  • ProfileId
  • VersionId

Our Recommendation of queries to create the indexes:-

db.getCollection("contentpageconfigentity").createIndex( { "PortalId": 1 } )
db.getCollection("contentpageconfigentity").createIndex( { "IsActive": 1 } )
db.getCollection("contentpageconfigentity").createIndex( { "ProfileId": 1 } )
db.getCollection("contentpageconfigentity").createIndex( { "VersionId": 1 } )

I. db.textwidgetentity.createIndex({LocaleId: 1, PortalId: 1, VersionId: 1})

TextWidgetEntity

a. Indexes needed

  • LocaleId
  • PortalId
  • VersionId

Our Recommendation of queries to create the indexes:-

db.getCollection("textwidgetentity").createIndex( { "LocaleId": 1 } )
db.getCollection("textwidgetentity").createIndex( { "PortalId": 1 } )
db.getCollection("textwidgetentity").createIndex( { "VersionId": 1 } )

J. db.webstoreentity.createIndex({PortalId: 1, LocaleId: 1, PublishState: 1})

WebstoreEntity

a. Indexes needed

  • PortalId
  • LocaleId
  • PublishState

Our Recommendation of queries to create the indexes:-

db.getCollection("webstoreentity").createIndex( { "PortalId": 1 } )
db.getCollection("webstoreentity").createIndex( { "LocaleId": 1 } )
db.getCollection("webstoreentity").createIndex( { "PublishState": 1 } )

Steps to create a batch file:

  1. Set the database name in createindex.js file (use < database name>)
  2. Move createindex.js and createindex.bat file in “C:\Program Files\MongoDB\Server\3.2\bin
  3. Run the createindex.bat file after publishing the store with the catalog to create the indexes in the database.

solid black circle