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

时间:2021-04-08 04:56:01

标签: node.js mongodb mongoose document

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

user -> departments -> projects

我经历过其他类似的 mongodb 查询,但没有一个能解决我的具体问题。

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

{
"_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 显示最近添加的项目,但由于用户与项目没有直接关系,我正在努力寻找一种方法来查询最近添加的前 3 个项目基于用户 ID 的项目,即 "_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 查询项目。我怎样才能做到这一点?感谢您的建议

1 个答案:

答案 0 :(得分:1)

您将使用 ObjectId() 将其 _id 包裹起来,并将其 createdAt 与 ISODate('') 包裹起来。我不知道为什么它在 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