Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sailsjs: automatically create composite unique index (mongodb)

I've the following model in my SailsJS application, I want to add composite unique key on the fields 'room_name' and 'school_id'.

What I currently do is run this command from mongo:

 db.room.ensureIndex({'room_name': 1, 'school_id':1}, {unique: true})

Question 1 Am I doing it right?

Question 2 Is it possible to modify my model so it automatically invokes this command without manually modifying the mongodb (from mongo command line)?

This is the model

module.exports = {

    schema: true,

    attributes: {

        room_name: {
            type: 'string',
            required: true
        },

        school_id: {
            type: 'string',
            required: true
        },

        children_count: {
            type: 'integer',
            required: true
        }
    }
   }
like image 413
user2867106 Avatar asked Apr 06 '14 22:04

user2867106


2 Answers

I created a sails hook to give advanced indexing options for models that use the sails-mongo adapter.

Supports all mongo indexing options.

https://www.npmjs.com/package/sails-hook-mongoat

like image 138
Salakar Avatar answered Oct 09 '22 22:10

Salakar


Sails does not currently (as of v0.10) support multi-key indexes, although it is on our radar. For the time being, the way you are doing it--by specifying the index directly in the Mongo console--is the correct (and only) way.

like image 42
sgress454 Avatar answered Oct 09 '22 21:10

sgress454