简体   繁体   中英

Mongoose text index on nested schema field

I have the following schema:

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

});

and I'm looking to create aa 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" } })

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...

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

you may have to recreate the index with the new parameters or try to drop and recreate the collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM