Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose create multiple indexes

I have one problem I wanted to implement full text search for mongodb, so i got stuck becouse in database created just one index for text search and i want to have multiple fields for search .. exmaple of schema :

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'
        }
      },

so what is the trick to do that ? how can i apply mutliple fields for full text search ? thx for anwser ...

like image 702
Maco Avatar asked May 27 '26 08:05

Maco


1 Answers

Try something like that:

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
like image 157
marcinn Avatar answered May 30 '26 11:05

marcinn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!