diff --git a/config/db/mongodb.js b/config/db/mongodb.js index 1ba8f648..5a5d2392 100644 --- a/config/db/mongodb.js +++ b/config/db/mongodb.js @@ -68,11 +68,21 @@ const DB = function() { return model; }; + const runCompoundIndex = function(modelName,opts) { + if (opts && opts.length > 0) { + for ( let indexPointer = 0 ; indexPointer < opts.length ; indexPointer++ ) { + let currentIndex = opts[indexPointer]; + db.collection(modelName).createIndex(currentIndex.name, currentIndex.indexType); + } + } + }; + return { database: db, createModel: createModel, ObjectId: ObjectId, - models: db.models + models: db.models, + runCompoundIndex: runCompoundIndex }; }; diff --git a/generics/abstract.js b/generics/abstract.js index 5a53eb5d..7c1d9453 100644 --- a/generics/abstract.js +++ b/generics/abstract.js @@ -14,6 +14,10 @@ let Abstract = class Abstract { constructor(schema) { database.createModel(schemas[schema]); + if ( schemas[schema].compoundIndex && schemas[schema].compoundIndex.length > 0 ) { + database.runCompoundIndex(schemas[schema].name,schemas[schema].compoundIndex); + } + } }; diff --git a/models/projects.js b/models/projects.js index 72e67ea2..35a91116 100644 --- a/models/projects.js +++ b/models/projects.js @@ -136,5 +136,15 @@ module.exports = { }, remarks : String, userProfile : Object - } + }, + compoundIndex: [ + { + "name" :{ userId: 1, solutionId: 1 }, + "indexType" : { unique: true, partialFilterExpression: { solutionId: { $exists: true }}} + } + ] + + + }; +