我有两个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相关问答推荐

node 上的磁盘压力

DocuSign:调用createEntaine时,RequestJWTApplicationToken返回401 AUTHORIZATION_INVALID_TOKEN

在Docker容器404页面中找不到服务器(提供静态reactjs文件)

类扩展值[object object]不是构造函数或null

mongodb首先自连接,然后根据某些条件与另一个表连接

dayjs的isSameOrAfter方法未按预期工作

未授权使用联合身份未授权用户角色从 Amplify graphQL 访问类型 Y 上的 X

我误解了外键的工作原理吗?使用续集

如何在没有云功能的情况下使用 Google PubSub

配额超出了每分钟的 Sheets API 写入请求数. node .js

在多个 .env 文件之间切换,例如 .env.development 和 node.js

使用 MongoDB 更新嵌套数组

如何运行用 TypeScript 编写的 Mocha 测试?

gyp WARN EACCES 用户root没有访问开发目录的权限

__dirname 未在 Node 14 版本中定义

需要 node-gyp 的 npm install 在 Windows 上失败

在 Node.js 中使用公钥加密数据

在 react-router v4 中使用 React IndexRoute

如何从 find 方法返回 Mongoose 结果?

如何创建安全(TLS/SSL)Websocket 服务器