外 collection 上的匹配条件与 mongoDB 中的查找
match condition on on foreign collection with lookup in mongoDB
我有 3 collections
users: [
{_id: "aaa", name: "John", department: 1},
{_id: "bbb", name: "Charles", department: 1},
{_id: "ccc", name: "Jessy", department: 1"},
{_id: "ddd", name: "Tim", department: 2},
{_id: "eee", name: "Max", department: 2},
{_id: "fff", name: "Julia", department: 2},
{_id: "ggg", name: "Arnold", department: 3}
]
departments: [
{_id: 1, name: "press", society: "times"},
{_id: 2, name: "news", society: "times"},
{_id: 3, name: "infos", society: "herald"}
]
society: [
{name: "times", country: "England"},
{name: "herald", country: "USA"}
]
一个用户在一个部门工作,一个部门在一个社会。
我想做 2 个请求,第一个是让所有来自“时代”社会的用户,第二个是让所有来自“英格兰”国家的用户。
我为第一个尝试了这个请求:
db.users.aggregate([
{'$match': {'dept.society': "times"}
{
'$lookup': {
'from': "departments",
'localField': "department",
'foreignField': "_id",
'as': "dept"
}
}])
但是因为条件是在国外collection(“部门”),好像不行。仅在本地 collection 条件下有效 ('department': 1)。我该怎么做,国家请求也是同样的问题吗?
两次查找即可完成
先获取society name times,再查找那两个集合
游乐场:https://mongoplayground.net/p/q-oCBjm-0hQ
db.society.aggregate([
{
"$match": {
"name": "times"
}
},
{
"$lookup": {
"from": "departments",
"localField": "name",
"foreignField": "society",
"as": "dept_society"
}
},
{
"$lookup": {
"from": "users",
"localField": "dept_society._id",
"foreignField": "department",
"as": "user_dept"
}
}
])
有很多方法可以实现你的两个查询。我还看到您想从 users
集合开始聚合。
这是一种查询方式:
all users from the society "times"
db.users.aggregate([
{
"$lookup": {
"from": "departments",
"localField": "department",
"foreignField": "_id",
"pipeline": [
{
"$match": {
"society": "times" // input society
}
}
],
"as": "deptLookup"
}
},
{ "$match": { "$expr": { "$gt": [ { "$size": "$deptLookup" }, 0 ] } } },
{ "$unset": "deptLookup" }
])
在 mongoplayground.net 上试用。
这是一种查询方式:
all users from the country "England"
db.users.aggregate([
{
"$lookup": {
"from": "departments",
"localField": "department",
"foreignField": "_id",
"pipeline": [
{
"$lookup": {
"from": "society",
"localField": "society",
"foreignField": "name",
"pipeline": [
{
"$match": {
"country": "England" // input country
}
}
],
"as": "socLookup"
}
},
{ "$match": { "$expr": { "$gt": [ { "$size": "$socLookup" }, 0 ] } } }
],
"as": "deptSocLookup"
}
},
{ "$match": { "$expr": { "$gt": [ { "$size": "$deptSocLookup" }, 0 ] } } },
{ "$unset": "deptSocLookup" }
])
在 mongoplayground.net 上试用。
我有 3 collections
users: [
{_id: "aaa", name: "John", department: 1},
{_id: "bbb", name: "Charles", department: 1},
{_id: "ccc", name: "Jessy", department: 1"},
{_id: "ddd", name: "Tim", department: 2},
{_id: "eee", name: "Max", department: 2},
{_id: "fff", name: "Julia", department: 2},
{_id: "ggg", name: "Arnold", department: 3}
]
departments: [
{_id: 1, name: "press", society: "times"},
{_id: 2, name: "news", society: "times"},
{_id: 3, name: "infos", society: "herald"}
]
society: [
{name: "times", country: "England"},
{name: "herald", country: "USA"}
]
一个用户在一个部门工作,一个部门在一个社会。
我想做 2 个请求,第一个是让所有来自“时代”社会的用户,第二个是让所有来自“英格兰”国家的用户。
我为第一个尝试了这个请求:
db.users.aggregate([
{'$match': {'dept.society': "times"}
{
'$lookup': {
'from': "departments",
'localField': "department",
'foreignField': "_id",
'as': "dept"
}
}])
但是因为条件是在国外collection(“部门”),好像不行。仅在本地 collection 条件下有效 ('department': 1)。我该怎么做,国家请求也是同样的问题吗?
两次查找即可完成
先获取society name times,再查找那两个集合
游乐场:https://mongoplayground.net/p/q-oCBjm-0hQ
db.society.aggregate([
{
"$match": {
"name": "times"
}
},
{
"$lookup": {
"from": "departments",
"localField": "name",
"foreignField": "society",
"as": "dept_society"
}
},
{
"$lookup": {
"from": "users",
"localField": "dept_society._id",
"foreignField": "department",
"as": "user_dept"
}
}
])
有很多方法可以实现你的两个查询。我还看到您想从 users
集合开始聚合。
这是一种查询方式:
all users from the society "times"
db.users.aggregate([
{
"$lookup": {
"from": "departments",
"localField": "department",
"foreignField": "_id",
"pipeline": [
{
"$match": {
"society": "times" // input society
}
}
],
"as": "deptLookup"
}
},
{ "$match": { "$expr": { "$gt": [ { "$size": "$deptLookup" }, 0 ] } } },
{ "$unset": "deptLookup" }
])
在 mongoplayground.net 上试用。
这是一种查询方式:
all users from the country "England"
db.users.aggregate([
{
"$lookup": {
"from": "departments",
"localField": "department",
"foreignField": "_id",
"pipeline": [
{
"$lookup": {
"from": "society",
"localField": "society",
"foreignField": "name",
"pipeline": [
{
"$match": {
"country": "England" // input country
}
}
],
"as": "socLookup"
}
},
{ "$match": { "$expr": { "$gt": [ { "$size": "$socLookup" }, 0 ] } } }
],
"as": "deptSocLookup"
}
},
{ "$match": { "$expr": { "$gt": [ { "$size": "$deptSocLookup" }, 0 ] } } },
{ "$unset": "deptSocLookup" }
])
在 mongoplayground.net 上试用。