ac360
ac360

Reputation: 7835

MongoDB w/ Mongoose - Where to put syntax to ensureIndex spanning multiple fields?

I'm trying to implement this solution and I'm not sure where to put it. I see the db variable called frequently, but I'm still new to node and mongoDb, so I don't know how to call it in my Model. Here is the syntax to ensure an index spanning multiple fields...

db.collection.ensureIndex( {
    description: "text",
    title: "text"
} );

Here is my model...

    //  Module dependencies.
var mongoose        = require('mongoose'),
    config          = require('../../config/config'),
    Schema          = mongoose.Schema,
    findOrCreate    = require('mongoose-findorcreate'),
    textSearch      = require('mongoose-text-search');

// Product Schema
var ProductSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    retailer: {
        type: String,
        required: true,
        trim: true
    },
    retailer_category: {
        type: String,
        required: true,
        trim: true
    },
    product_id: {
        type: String,
        required: true,
        trim: true
    },
    link: {
        type: String,
        trim: true
    },
    title: {
        type: String,
        trim: true
    },
    price: {
        type: Number
    },
    // Rating - 0 out of 5 (can be decimal)
    rating: {
        type: Number
    },
    description: {
        type: String,
        trim: true
    },
    variations: {
        type: Schema.Types.Mixed,
        default: []
    },
    images: {
        type: Boolean,
        default: false
    }
}); 

// Validations
ProductSchema.index({ retailer: 1, product_id: 1 }, { unique: true });

// Statics
ProductSchema.statics = {
    load: function(id, cb) {
        this.findOne({
            _id: id
        }).exec(cb);
    }
};

// Plug-Ins
ProductSchema.plugin(findOrCreate);
ProductSchema.plugin(textSearch);

mongoose.model('Product', ProductSchema);

Upvotes: 4

Views: 1895

Answers (2)

K. Francis
K. Francis

Reputation: 341

I scratched my head over this one too. After digging around the mongoose test cases, I found that ensureIndex resides in a mongoose model's collection property.

var ProductModel = mongoose.model('Product', ProductSchema);
ProductModel.collection.ensureIndex({
    description : 'text',
    title : 'text'
}, function(error, res) {
    if(error){
        return console.error('failed ensureIndex with error', error);
    }
    console.log('ensureIndex succeeded with response', res);
});    

Note that a callback is required, or Mongo will throw the error:

Error: Cannot use a writeConcern without a provided callback

Upvotes: 1

adrichman
adrichman

Reputation: 1235

var Product = mongoose.model('Product', ProductSchema);

Product.ensureIndexes( function(err) { 
    if (err) { 
      console.log(err); 
    } 
})

It's worth noting:

When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false.

from http://mongoosejs.com/docs/guide.html

Upvotes: 1

Related Questions