Thursday, August 25, 2016

Mongoose text index on nested schema field

Leave a Comment

I have the following schema:

const Schema = ({   metadata: {     title: String,     ...   },   ...  }); 

and I'm looking to create a a text index on metadata.title. I can create a text index successfully on any first level property, but I'm running into trouble with the nested title.

I've tried the following code, to no avail. Is my syntax wrong? I've had no luck with docs...

Schema.index({ 'metadata.title': 'text' }); 

Searching:

Schema   .find(     { $text : { $search : req.params.query } },     { score : { $meta: "textScore" } }) 

2 Answers

Answers 1

It turns out what I had originally was correct, as pointed out by @JohnnyHK. I must have had some other error that caused the index to not work...

Answers 2

const Schema = ({    metadata: {      title: {        type: String,        index: true        }      ...    },    ...    });

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment