Home > database >  How to add field in array with aggregate mongoose from another array coming from previous stage
How to add field in array with aggregate mongoose from another array coming from previous stage

Time:07-30

I have two mongoose schema one for keywords :

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 }
);

and one for keywordsUser :

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 }
);

I want to get keywords by user grouped by type which I calculate from wrongAnswer and correctAnswer

I try to do this using aggregate

  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,
    },
  },
]);

I have this result from aggregate

returned data from. postman

I want to add new aggregate stage to get this result

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"
            }
        ]

CodePudding user response:

You can just populate with the keywordId not with aggregate

  • Related