使用顶级文档 ID 查询深层嵌套文档

Query deep level nested documents with top level document id

我有三个级别的集合,即用户、部门和项目。这是它的结构,每个用户将有多个部门,每个部门将有多个项目。

user -> departments -> projects

我已经完成了其他类似的 mongodb 查询,但其中 none 正在解决我的具体问题。

这是我的整个数据结构的样子

{
"_id": "5a2ca2227c42ad67682731d4", // this is user id
"name": "XYZ Bank",
createdAt: "2021-04-08T04:05:30.340Z",
updatedAt: "2021-04-08T04:05:30.340Z",
"departments": [
    {
        "_id": "5a2ca2227c42ad6768271232",
        "name": "Digital",
        "userId": "5a2ca2227c42ad67682731d4",
        createdAt: "2021-04-08T04:05:30.340Z",
        updatedAt: "2021-04-08T04:05:30.340Z",
        "projects": [
            {
                "_id": "5a2ca2227c42ad6768274343",
                "name": "ABC",
                "departmentId": "5a2ca2227c42ad6768271232",
                createdAt: "2021-04-08T04:05:30.340Z",
                updatedAt: "2021-04-08T04:05:30.340Z",
            },
            {
                "_id": "5a2ca2227c42ad6768274344",
                "name": "XYZ",
                "departmentId": "5a2ca2227c42ad6768271232",
                createdAt: "2021-04-08T04:05:30.340Z",
                updatedAt: "2021-04-08T04:05:30.340Z",
            },
            {
                "_id": "5a2ca2227c42ad6768274345",
                "name": "BCA",
                "departmentId": "5a2ca2227c42ad6768271232",
                createdAt: "2021-04-08T04:05:30.340Z",
                updatedAt: "2021-04-08T04:05:30.340Z",
            }
        ]
    },
    {
        "_id": "5a2ca2227c42ad6768271233",
        "name": "Finance",
        "userId": "5a2ca2227c42ad67682731d4",
        createdAt: "2021-04-08T04:05:30.340Z",
        updatedAt: "2021-04-08T04:05:30.340Z",
        "projects": [
            {
                "_id": "5a2ca2227c42ad6768274346",
                "name": "DSE",
                "departmentId": "5a2ca2227c42ad6768271233",
                createdAt: "2021-04-08T04:05:30.340Z",
                updatedAt: "2021-04-08T04:05:30.340Z",
            },
            {
                "_id": "5a2ca2227c42ad6768274347",
                "name": "ADB",
                "departmentId": "5a2ca2227c42ad6768271233",
                createdAt: "2021-04-08T04:05:30.340Z",
                updatedAt: "2021-04-08T04:05:30.340Z",
            },
            {
                "_id": "5a2ca2227c42ad6768274348",
                "name": "QAW",
                "departmentId": "5a2ca2227c42ad6768271233",
                createdAt: "2021-04-08T04:05:30.340Z",
                updatedAt: "2021-04-08T04:05:30.340Z",
            }
        ]
    }
]
}

我正在努力解决的场景是我必须根据用户 ID 显示最近添加的项目,但由于用户与项目没有直接关系,我正在努力寻找一种方法来查询最近添加的项目基于用户 ID 的前 3 个项目,即 "_id": "5a2ca2227c42ad67682731d4"

我一般查询直接关系是

export async function getProjectsByDepartmentId (
  req: Request,
  res: Response
): Promise<any> {
  try {
    const query = {
      departmentId: req.query.departmentId,
    };
    const aggregators = [];
    aggregators.push({
      $facet: {
        data: [
          { $match: query },
          { $sort: { _id: -1 } },
          { $limit: req.query.limit },
        ],
        total: [{ $count: "count" }],
      },
    });

    const projects = await Project.aggregate(aggregators);
    return res.status(200).send(projects);
  } catch (err) {
    return res.status(500).send(err || err.message);
  }
}

我期望的输出是

{
    "_id": "5a2ca2227c42ad67682731d4", // this is user id
    "name": "XYZ Bank",
    createdAt: "2021-04-08T04:05:30.340Z",
    updatedAt: "2021-04-08T04:05:30.340Z",
    "projects": [
                {
                    "_id": "5a2ca2227c42ad6768274343",
                    "name": "ABC",
                    "departmentId": "5a2ca2227c42ad6768271232",
                    createdAt: "2021-04-08T04:05:30.340Z",
                    updatedAt: "2021-04-08T04:05:30.340Z",
                },
                {
                    "_id": "5a2ca2227c42ad6768274344",
                    "name": "XYZ",
                    "departmentId": "5a2ca2227c42ad6768271232",
                    createdAt: "2021-04-08T04:05:30.340Z",
                    updatedAt: "2021-04-08T04:05:30.340Z",
                },
                {
                    "_id": "5a2ca2227c42ad6768274345",
                    "name": "BCA",
                    "departmentId": "5a2ca2227c42ad6768271232",
                    createdAt: "2021-04-08T04:05:30.340Z",
                    updatedAt: "2021-04-08T04:05:30.340Z",
                }
            ]
    ]
}

但是不知道如何根据没有直接关系的userId查询项目。我怎样才能做到这一点?感谢您的建议

您将用 ObjectId() 包装它的 _id,用 ISODate('') 包装它的 createdAt。我不知道为什么它在 mongoplayground

中不起作用
db.collection.aggregate([
  {
    $match: {
      _id: "5a2ca2227c42ad67682731d4"
    }
  },
  {
    $unwind: "$departments"
  },
  {
    $unwind: "$departments.projects"
  },
  {
    $match: {
      "departments.projects.createdAt": {
        $gte: "2021-04-09T00:00:00Z"
      }
    }
  }
])

MongoPlayground:https://mongoplayground.net/p/Qll_t6RBltL