gpt4 book ai didi

node.js - Mongoose 创建多个索引并个性化查询以调用特定索引

转载 作者:可可西里 更新时间:2023-11-01 10:25:43 27 4
gpt4 key购买 nike

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

这是我的模型

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

我想要这些索引:

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

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

最佳答案

在您的 module.exports 行之前:

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

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

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

关于node.js - Mongoose 创建多个索引并个性化查询以调用特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com