我有两个mongoose 模式,一个用于关键字:

const keywordSchema = new Schema<keywordI>(
  {
    sentence: { type: String, required: true },
    example: { type: String, required: true },
    translate: { type: String, required: true },
    imageUrl: { type: String, required: true },
    audioUrl: { type: String, required: true },
    deletedAt: { type: Date, select: false },
  },
  { timestamps: true }
);

一个用于关键字Suser:

const keywordUserSchema = new Schema<keywordUserI>(
  {
    keywordId: {
      type: mongoose.Types.ObjectId,
      ref: "Keyword",
      required: true,
    },
    userId: {
      type: mongoose.Types.ObjectId,
      ref: "User",
      required: true,
    },
    isBookMarked: { type: Boolean, default: false },
    correctAnswer: { type: Number, default: 0 },
    wrongAnswer: { type: Number, default: 0 },
  },
  { timestamps: true }
);

我想按用户获得关键字,按类型分组,根据错误答案和正确答案计算

我try 使用聚合来实现这一点

  return KeyWordUser.aggregate([
  {
    $match: { userId },
  },
  {
    $addFields: {
      type: {
        $function: {
          body: getType,
          args: ["$correctAnswer", "$wrongAnswer"],
          lang: "js",
        },
      },
    },
  },
  {
    $group: {
      _id: "$type",
      words: {
        $push: "$keywordId",
      },
      isBookMarked: {
        $push: { isBookMarked: "$isBookMarked", keywordId: "$keywordId" },
      },
    },
  },
  {
    $lookup: {
      localField: "words",
      from: "keywords",
      foreignField: "_id",
      as: "props",
    },
  },
  {
    $addFields: { type: "$_id" },
  },
  {
    $project: {
      _id: 0,
      words: 0,
    },
  },
]);

我从汇总中得到了这个结果

returned data from. postman

我想添加新的聚合阶段以获得此结果

previous result

[
            {
                "isBookMarked": [
                    {
                        "keywordId": "62e2a0ad3113b8234511cd72"
                    },
                    {
                        "isBookMarked": false,
                        "keywordId": "62e2a0ba3113b8234511cd74"
                    }
                ],
                "props": [
                    {
                        "_id": "62e2a0ad3113b8234511cd72",
                        "sentence": "hello",
                        "example": "Hello , what's your name",
                        "translate": "مرحبا ما اسمك",
                        "imageUrl": "src/img/keywords/keyword-A7H_M-1659019437698.png",
                        "audioUrl": "src/audio/keywords/keyword-YyhUp-1659019437700.mpeg",
                        "createdAt": "2022-07-28T14:43:57.708Z",
                        "updatedAt": "2022-07-28T14:43:57.708Z",
                        "__v": 0
                    },
                    {
                        "_id": "62e2a0ba3113b8234511cd74",
                        "sentence": "hello 2 ",
                        "example": "Hello , what's your name 2 ",
                        "translate": "مرحبا ما اسمك 2",
                        "imageUrl": "src/img/keywords/keyword-2JO7D-1659019450396.png",
                        "audioUrl": "src/audio/keywords/keyword-kt5Tc-1659019450397.mpeg",
                        "createdAt": "2022-07-28T14:44:10.400Z",
                        "updatedAt": "2022-07-28T14:44:10.400Z",
                        "__v": 0
                    }
                ],
                "type": "strong"
            }
        ]

and i want this result :

[
            {
   
                "props": [
                    {
                        "_id": "62e2a0ad3113b8234511cd72",

                        "sentence": "hello",
                        "example": "Hello , what's your name",
                        "translate": "مرحبا ما اسمك",
                        "imageUrl": "src/img/keywords/keyword-A7H_M-1659019437698.png",
                        "audioUrl": "src/audio/keywords/keyword-YyhUp-1659019437700.mpeg",
                        "createdAt": "2022-07-28T14:43:57.708Z",
                        "updatedAt": "2022-07-28T14:43:57.708Z",
                        "__v": 0
                    },
                    {
                        "_id": "62e2a0ba3113b8234511cd74",
                        "isBookMarked" : false 
                        "sentence": "hello 2 ",
                        "example": "Hello , what's your name 2 ",
                        "translate": "مرحبا ما اسمك 2",
                        "imageUrl": "src/img/keywords/keyword-2JO7D-1659019450396.png",
                        "audioUrl": "src/audio/keywords/keyword-kt5Tc-1659019450397.mpeg",
                        "createdAt": "2022-07-28T14:44:10.400Z",
                        "updatedAt": "2022-07-28T14:44:10.400Z",
                        "__v": 0
                    }
                ],
                "type": "strong"
            }
        ]

推荐答案

你可以用populatekeywordId,不用聚合

Node.js相关问答推荐

try 使用Puppeteer抓取Twitter时数组空

在Node JS中获取控制台选项卡标题

如何在Firebase Cloud Function v2计划函数中设置代码中的时区?

购物车是空的状态|本地存储不更新产品数量|Redux|

带有apache Couch-db和Nano的推荐引擎:过滤特定用户的视图

Node js 处理回调和 Promise

如何从mongoose 对象内嵌套的数组中提取数组元素?

如何在带有 JS 的 Nodejs 中使用没有 Async 方法的 Await

Typescript 条件语句不过滤值?

在系统启动时启动本地 node 服务器

React Native:执行 com.android.build.gradle.internal.tasks.Workers$ActionFacade 时发生故障

Electron 模板(Typescript + Webpack)中的这个 Electron Forge ERROR 是什么?

Zod 模式中的self 数组

带有 node.js 和 express 的基本网络服务器,用于提供 html 文件和assets资源

`npm install` 以Killed结尾

如何让should.be.false语法通过 jslint?

fs.createWriteStream 不会立即创建文件?

如何仅在丢失的路由上将 Express.js 设置为 404?

node.js 异步库

避免在弹性 beantalk 中重建 node_modules