首页 > 解决方案 > MongoDb 聚合全文搜索不返回 id 字段

问题描述

我在我的 Mongo 数据库中实现了全文搜索。当我尝试使用聚合搜索我的索引时,结果很好,但缺少 id。

models.Piece.aggregate([
    {
      $searchBeta: {
        "index": "search-piece",
        "search": {
          "query": search,
          "path": ["name", "styles"],
          "phrase": { prefix: true },
        },
      },
    },
    {
      $skip: offset,
    },
    {
      $limit: limit,
    },
  ]);

在此处输入图像描述

标签: node.jsmongodbmongoosefull-text-searchaggregate

解决方案


我解决问题。问题来自 graphql 和 mongodb 聚合。我必须为 id 字段编写特定的解析。

  Piece: {
    id: (parent, args, { models }) => parent._id,
  },

推荐阅读