【问题标题】:Mongoose create multiple indexesMongoose 创建多个索引
【发布时间】:2015-03-17 10:56:42
【问题描述】:

我有一个问题,我想为 mongodb 实现全文搜索,所以我被卡住了,因为在数据库中只为文本搜索创建了一个索引,我想有多个搜索字段.. 模式示例:

name: {
    type: String,
    required: true,
    trim: true,
    index: 'text'
  },
  sub_name: {
    type: String,
    trim: true

  },
  location: [{
      geo: {
        type: [Number],
        index: '2d'
      },
      description: {
        type: String,
        trim: true,
        index: 'text'
      },
      address: {

        city: {
          type: String,
          index: 'text'
        },

        street: {
          type: String,
          index: 'text'
        },
        state: {
          type: String,
          index: 'text'
        }
      },

那么这样做的诀窍是什么?如何应用多个字段进行全文搜索? thx for anwser ...

【问题讨论】:

    标签: mongodb express indexing mongoose


    【解决方案1】:

    试试这样的:

    var schema= new Schema({
      name: String,
      sub_name: String,
      tags: { type: [String], index: true } // field level
    });
    
    schema.index({ name: 'text', sub_name: 'text' }); // schema level
    

    【讨论】:

    • 我如何在描述中应用索引? schema.index({ 'location.description': 'text', type: 'text' }); ?
    • 我从来没有在 mongoose 的嵌套字段上创建索引,但因为这是你在 mogno shell 中创建索引的方式(通过指定路径),我想这也是它在 mongoose 中应该如何工作的方式。 schemaObj.index({ 'location.description': 'text', type: 'text' });
    【解决方案2】:

    创建多个文本索引使用这个。这将有助于全文搜索

    1. 查找索引:db.collectionName.getIndexes();

    2. 比删除索引名称:db.campaigns.dropIndex('name_text');

    3. 在多个键上创建索引:db.campaigns.ensureIndex({ name: 'text' , description: 'text', tags : 'text' });

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 2019-12-18
      • 2012-09-16
      • 2015-01-09
      • 2016-03-20
      • 2023-03-26
      • 2017-09-01
      • 1970-01-01
      相关资源
      最近更新 更多