【问题标题】:Mongoose create multiple index and personalize quety to call with specific indexMongoose 创建多个索引并个性化 quety 以使用特定索引调用
【发布时间】:2016-03-20 17:38:18
【问题描述】:

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

这是我的模特

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);

我想要这些索引:

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

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

【问题讨论】:

    标签: node.js mongodb express mongoose mongoose-schema


    【解决方案1】:

    在您的module.exports 行之前:

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

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

    Thing.find({...}).hint({word: 1});
    

    【讨论】:

      猜你喜欢
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      相关资源
      最近更新 更多