在猫鼬中按孙 属性 过滤
Filtering By Grandchild property in mongoose
我对猫鼬还很陌生,我想知道我是否可以根据孙子 属性 进行过滤。我到处都看过,但无法根据我正在尝试做的事情找到类似的问题。这是场景:
假设我有这样一个数据库:
db={
"parents": [
{
"_id": ObjectId("5a834e000102030405000000"),
"child": ObjectId("5a934e000102030405000000")
},
{
"_id": ObjectId("5a834e000102030405000001"),
"child": ObjectId("5a934e000102030405000001")
},
{
"_id": ObjectId("5a834e000102030405000002"),
"child": ObjectId("5a934e000102030405000002")
},
],
"children": [
{
"_id": ObjectId("5a934e000102030405000000"),
"grandchild": ObjectId("5a734e000102030405000000")
},
{
"_id": ObjectId("5a934e000102030405000001"),
"grandchild": ObjectId("5a734e000102030405000001")
},
{
"_id": ObjectId("5a934e000102030405000002"),
"grandchild": ObjectId("5a734e000102030405000002")
}
],
"grandchildren": [
{
"_id": ObjectId("5a734e000102030405000000"),
"name": "grandchild1"
},
{
"_id": ObjectId("5a734e000102030405000001"),
"name": "grandchild2"
},
{
"_id": ObjectId("5a734e000102030405000002"),
"name": "grandchild3"
}
]
}
我想 return 所有 parent 有孙子名字为“grandchild1”的人。
类似于
$match: {
"child.grandchild.name": "grandchild1"
}
所以只有这个 parent 会在结果中 returned --
[{
"_id": ObjectId("5a834e000102030405000000"),
"child": ObjectId("5a934e000102030405000000")
},]
我在任何人回复之前就找到了答案...
https://mongoplayground.net/p/w1kw58PzCyk
db.parents.aggregate([
{
$lookup: {
from: "children",
localField: "child",
foreignField: "_id",
as: "child",
}
},
{
$addFields: {
child: {
$arrayElemAt: [
"$child",
0
]
}
}
},
{
"$lookup": {
from: "grandchildren",
localField: "child.grandchild",
foreignField: "_id",
as: "grandchild",
}
},
{
$addFields: {
grandchild: {
$arrayElemAt: [
"$grandchild",
0
]
}
}
},
{
$match: {
"grandchild.name": "grandchild1"
}
}
])
我对猫鼬还很陌生,我想知道我是否可以根据孙子 属性 进行过滤。我到处都看过,但无法根据我正在尝试做的事情找到类似的问题。这是场景:
假设我有这样一个数据库:
db={
"parents": [
{
"_id": ObjectId("5a834e000102030405000000"),
"child": ObjectId("5a934e000102030405000000")
},
{
"_id": ObjectId("5a834e000102030405000001"),
"child": ObjectId("5a934e000102030405000001")
},
{
"_id": ObjectId("5a834e000102030405000002"),
"child": ObjectId("5a934e000102030405000002")
},
],
"children": [
{
"_id": ObjectId("5a934e000102030405000000"),
"grandchild": ObjectId("5a734e000102030405000000")
},
{
"_id": ObjectId("5a934e000102030405000001"),
"grandchild": ObjectId("5a734e000102030405000001")
},
{
"_id": ObjectId("5a934e000102030405000002"),
"grandchild": ObjectId("5a734e000102030405000002")
}
],
"grandchildren": [
{
"_id": ObjectId("5a734e000102030405000000"),
"name": "grandchild1"
},
{
"_id": ObjectId("5a734e000102030405000001"),
"name": "grandchild2"
},
{
"_id": ObjectId("5a734e000102030405000002"),
"name": "grandchild3"
}
]
}
我想 return 所有 parent 有孙子名字为“grandchild1”的人。
类似于
$match: {
"child.grandchild.name": "grandchild1"
}
所以只有这个 parent 会在结果中 returned --
[{
"_id": ObjectId("5a834e000102030405000000"),
"child": ObjectId("5a934e000102030405000000")
},]
我在任何人回复之前就找到了答案...
https://mongoplayground.net/p/w1kw58PzCyk
db.parents.aggregate([
{
$lookup: {
from: "children",
localField: "child",
foreignField: "_id",
as: "child",
}
},
{
$addFields: {
child: {
$arrayElemAt: [
"$child",
0
]
}
}
},
{
"$lookup": {
from: "grandchildren",
localField: "child.grandchild",
foreignField: "_id",
as: "grandchild",
}
},
{
$addFields: {
grandchild: {
$arrayElemAt: [
"$grandchild",
0
]
}
}
},
{
$match: {
"grandchild.name": "grandchild1"
}
}
])