嵌套模式字段上的Mongoose文本索引

时间:2022-04-04 19:42:10

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.

我正在寻找在metadata.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 个解决方案

#1


2  

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

事实证明,正如@JohnnyHK所指出的,我原来的是正确的。我一定有一些其他错误导致索引不起作用......

#2


0  

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

});

#1


2  

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

事实证明,正如@JohnnyHK所指出的,我原来的是正确的。我一定有一些其他错误导致索引不起作用......

#2


0  

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

});