【发布时间】:2021-09-28 21:19:59
【问题描述】:
我有这样的 MongoDB 文档:
{
_id: {"$oid":"object_id"},
email: "email@example.com"
password: "password_hash"
info: {
birthday: "1993-03-12"
...
}
...
}
而且我无法改变它。我需要创建 Mongo 查询以按年龄(0-18 岁和 18 岁以上)对用户进行分组。 我不知道怎么处理。我只创建了从生日日期返回年份的查询(这是字符串):
db.users.aggregate(
[
{
$project: {
year: { $year: { date: { $dateFromString: { dateString: "$info.birthday", format: "%Y-%m-%d" } } } }
}
}
]
)
但我不确定我接下来应该怎么做。
【问题讨论】:
标签: mongodb mongodb-query aggregate