2

我想对我的模型做很多索引,当我做查询时,在那个查询中使用一个特定的索引

这是我的模型

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var ThingSchema = new Schema({

        word:{
            type: 'ObjectId',
            required:true
        },
        frecuency:{
            type: String,
            default:'enabled'
        },
        document:{
            documentId:{
                type: 'ObjectId'
            },
            quality:{
                type: Number
            }
        },
        location: {
            type: [Number],
            index: '2d' 
        },
        createdAt: {
            type: Date,
            default: Date.now
        },
        updatedAt: {
            type: Date,
            default: Date.now
        }
    });

module.exports = mongoose.model('Thing', ThingSchema);

我想要这些索引:

  • 按单词索引(是一个字符串)
  • 按位置索引(是地理索引)
  • 按单词和位置索引

现在,当我进行查询时,我想指定要使用的索引

4

1 回答 1

2

在您的module.exports行之前:

ThingSchema.index({word: 1});
// all other indexes you want to add...

当需要进行查询时,使用hint()指定要使用的索引:

Thing.find({...}).hint({word: 1});
于 2015-12-15T07:04:14.527 回答