给出一个mongoose 物体.如何在Document Object中执行全文搜索

 {
    "product" :[
        {
            "_id": "633d32c4ea9b06177e183388",
            "document": {
                "_id": "633d32c4ea9b06177e183388",
                "category": [
                    "62bacb137e6e0b6ac850b9ce",
                    "633d19260b72ba127d928e74"
                ],
                "name": "Iphone 13",
                "slug": "iphone-13",
                "__v": 0,
                "price": 2000.5,
                "images": [
                    {
                        "_id": "63cca1a271c3635246eee937",
                        "url": "https://res.cloudinary.com/..."
                    }
                ],
                "sold": 15,
                "ratings": []
            },
            "avgRating": null
        },
    ...

]


}

这就是我试过的方法,但不起作用

const product = await Product.aggregate([
  {
    $project: {
      document: "$$ROOT",
      avgRating: {
        $floor: { $avg: "$ratings.star" },
      },
    },
  },

  {
    $match: {
      document: { $text: { $search: `"${searchText}"` } },
    },
  },
]);

这是我的产品型号

const product = {
  name: {
    type: String,
    required: true,
    trim: true,
    minlength: [2, "Too short"],
    maxlength: [32, "Too long"],
    unique: true,
  },
  slug: {
    type: String,
    unique: true,
    lowercase: true,
    index: true,
  },
  category: [
    {
      type: ObjectId,
      ref: "Category",
    },
  ],
  description: {
    type: String,
    maxlength: 200,
  },
  price: {
    type: Number,
    required: true,
    trim: true,
    maxlength: 32,
  },
  shipping: {
    type: String,
    enum: ["Yes", "No"],
  },
  color: [
    {
      type: String,
      enum: ["Black", "Brown", "Silver", "White", "Blue"],
    },
  ],
  sold: {
    type: Number,
    default: 0,
  },
  quantity: {
    type: Number,
  },
  images: [
    {
      public_id: String,
      url: String,
    },
  ],
  ratings: [
    {
      star: Number,
      postedBy: {
        type: ObjectId,
        ref: "User",
      },
    },
  ],
};

我们的目标是通过获取产品评级的平均值来汇总产品评级,然后链接多个WHERE子句,其中一个WHERE子句是在文档对象中执行全文搜索.

敬请指教.

谢谢.

推荐答案

$text要求字段的分数为text index,其内容将被搜索.在您的例子中,您在文档上使用的是$text,而不是文本字段,因此它很可能会产生意外的结果.解决这个问题的一种方法是在模式中添加一个字段(假设是textContent),这是文档的字符串版本,然后在该字段上创建text index.所以你的管道会变成这样:

const product = await Product.aggregate([
  {
    $match: {
      textContent: { $text: { $search: `"${searchText}"` } },
    },
  },
  {
    $project: {
      avgRating: {
        $floor: { $avg: "$ratings.star" },
      },
    },
  },
]);

注意:我将$Match移到了上面,因为预计它将是管道的第一阶段.请参阅此处的$text个文档.

Node.js相关问答推荐

如何使用Node.js、Express和Mongoose创建多个API

Mongoose-如何从父文档填充到子文档

MongooseError[MissingSchemaError]:尚未为模型注册架构

通过 Node js 中的 TBA 执行 netsuite REST upsert 操作出现 401 错误

我需要聚合两个 MongoDB 集合

eSignature API 的 NodeJS SDK 是否支持数据流?

为什么 client.on("messageCreate") 的 TextChannel 中缺少 nsfw 属性?

Fly.io 启动问题:无法从源获取图像或构建错误:构建错误:连接期间出错:发布...

更新文档数组中的文档 Mongoose

如何让 vscode 假设一个 node.js 上下文,以便它不假设 `fetch` 存在?

users.watch(在 gmail google api 中)如何收听通知?

在 PassportJS 中使用多种本地策略

node.js(ES6 / Babel)中 import X 和 import * as X 的区别?

Nodejs续集批量更新

在 react-router v4 中使用 React IndexRoute

AWS Lambda 函数写入 S3

为什么 Node.js 没有原生 DOM?

mongoose 填充与对象嵌套

promise 回调返回promise

CORS 错误:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段授权