【问题标题】:MongoDb aggregate full text search doesn't return id fieldMongoDb 聚合全文搜索不返回 id 字段
【发布时间】:2020-05-20 13:11:11
【问题描述】:

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

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

enter image description here

【问题讨论】:

  • 缺少 id 是什么意思?是关于_id 还是id:null

标签: node.js mongodb mongoose full-text-search aggregate


【解决方案1】:

使用 $project 作为结束阶段。投影所需的字段。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2020-12-08
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多