Why do i miss data when i use $project

hi,
does anyone have an idea, what’s the reason for missing data in my aggregation?
This is my pipeline:

[
  {
    $lookup: {
      as: "resultcheck",
      foreignField: "checkId",
      from: "checks",
      localField: "checkId",
    },
  },
  {
    $lookup: {
      as: "resultDepartment",
      foreignField: "DepId",
      from: "department",
      localField: "DepartmentId",
    },
  },
  {
    $unwind: {
      path: "$X",
    },
  },
  {
    $unwind: {
      path: "$X.X",
    },
  },
  {
    $unwind: {
      path: "$resultDepartment",
    },
  },
  {
    $unwind: {
      path: "$resultcheck",
    },
  },
  {
    $match: {
      $and: [
        {
          "X.X.TotalType":
            "Forecast",
          "X.X.FiscalYear":
            {
              $ne: null,
            },
        },
      ],
    },
  },
  {
    $project: {
      _id: true,
      number: "$Number",
      responsible: "$rResponsible",
      calendarMonth: "$Period",
      department: "$resultDepartment.Name",
      finishDate: "$FinishDate",
      xYear:   "$X.X.Year",
      year: "$Year",
      level: "$Level",
      check: "$resultcheck.check",
      period: {
        $switch: {
          branches: [
            {
              case: {
                $eq: ["$Period", 10],
              },
              then: 1,
            },
            {
              case: {
                $eq: ["$Period", 11],
              },
              then: 2,
            },
            {
              case: {
                $eq: ["$Period", 12],
              },
              then: 3,
            },
            {
              case: {
                $eq: ["$Period", 1],
              },
              then: 4,
            },
            {
              case: {
                $eq: ["$Period", 2],
              },
              then: 5,
            },
            {
              case: {
                $eq: ["$Period", 3],
              },
              then: 6,
            },
            {
              case: {
                $eq: ["$Period", 4],
              },
              then: 7,
            },
            {
              case: {
                $eq: ["$Period", 5],
              },
              then: 8,
            },
            {
              case: {
                $eq: ["$Period", 6],
              },
              then: 9,
            },
            {
              case: {
                $eq: ["$Period", 7],
              },
              then: 10,
            },
            {
              case: {
                $eq: ["$Period", 8],
              },
              then: 11,
            },
            {
              case: {
                $eq: ["$Period", 9],
              },
              then: 12,
            },
          ],
        },
      },
      title: "$Title",
      type: "$X.X.TotalType",
      value:
        "$X.X.Value",
    },
  },
]

i’ve got from over 17k documents only 16k.
The original document contains a lot of objects with arrays inside the objects, which i resolve with unwind i.e.

Can someone help?

Kind Regards
LJ

PS: i have anonymized some parts of this code

Project should not limit documents output, have you commenting out all stages and gradually running and counting document with each stage gradually introduced?

almost done. If all is commented out i’ve got all docs.
I also checked more than one time the lookups, to be sure that nothin is happen there. Could not find any issues after lookup or match

Your switch statement could also be simplified using the modulus operator:

db.getCollection("numbers").aggregate([
{
    $addFields:{
        newNum:{
            $add:[
                {
                    $mod:[
                        {$add:['$num', 2]},
                        12
                    ]
                },
                1
            ]
        }            
    }
}
])

Check the following $unwind option:

1 Like

It seems to be an issue with the X.X Values which where needed.

i changed the $unwind to:

{
$unwind: {
path: "myPath",
includeArrayIndex: "string",
preserveNullAndEmptyArrays: true,
},
}

, changed the match statement to 2 single statements and the missing data popped up and where available now…

Thy very much :slight_smile:

1 Like

i will try, thy for the tip