1

I have the following schema:

entrySchema = new mongoose.Schema({

    size:    { type: Number },

    title:   {type: String, trim: true },
    content: { type: String, trim: true },

    tags:    { type: [String], trim: true, index: true },
    author:  { type: String, trim: true, index: true }
  });


entrySchema.index({ title: "text", content: "text" });

module.exports = mongoose.model('Entry', entrySchema);

The problem is that mongoose does not create the text indexes. The indexes for tags and author are created correctly, though.

Am I using the index() function in a wrong way?

I don't get any errors in the mongod session. It logs successful index creation for the non-text indexes, but it seems as if mongoose never calls ensureIndex for the text indexes.

codingFriend1
  • 6,487
  • 6
  • 45
  • 67
  • 1
    I think you can't create a compound index with two text indexes: "A compound text index cannot include any other special index types, such as multi-key or geospatial index fields" (http://docs.mongodb.org/manual/core/index-text/#text-index-compound) – joao Feb 15 '15 at 12:44
  • @joao as I understood that is exactly the way to go, as described in http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/ – codingFriend1 Feb 15 '15 at 13:26
  • you are completely right. – joao Feb 15 '15 at 13:35
  • 2
    See this answer for some info on how to debug this: http://stackoverflow.com/questions/12452865/mongoose-not-creating-indexes/12453041#12453041 – JohnnyHK Feb 15 '15 at 16:05

1 Answers1

0

After debugging as described in Mongoose Not Creating Indexes (thanks to @JohnyHK for the link) I saw that the actual problem was not the text index.

I was using the mongoose-auto-increment plugin and that resulted in errors indexing the _id field. The solution was to have autoIncrement not use the _id field but a separate field like this:

entrySchema.plugin autoIncrement.plugin, {
    model: 'Entry'
    startAt: 1000
    field: 'shortId'
}

I just did not thing about that because indexing worked fine without the text index. There seems to be some kind of incompatibility with the plugin and text indexes.

Community
  • 1
  • 1
codingFriend1
  • 6,487
  • 6
  • 45
  • 67