从另一个集合中获取数据(字符串 -> ObjectId)
Get Data from another collection (string -> ObjectId)
假设我有这两个集合:
// Members:
{
"_id":{
"$oid":"60dca71f0394f430c8ca296d"
},
"church":"60dbb265a75a610d90b45c6b",
"name":"Julio Verne Cerqueira"
},
{
"_id":{
"$oid":"60dca71f0394f430c8ca29a8"
},
"nome":"Ryan Steel Oliveira",
"church":"60dbb265a75a610d90b45c6c"
}
和
// Churches
{
"_id": {
"$oid": "60dbb265a75a610d90b45c6c"
},
"name": "Saint Antoine Hill",
"active": true
},
{
"_id": {
"$oid": "60dbb265a75a610d90b45c6b"
},
"name": "Jackeline Hill",
"active": true
}
我想查询它并得到这样的结果:
// Member with Church names
{
"_id":{
"$oid":"60dca71f0394f430c8ca296d"
},
"church":"Jackeline Hill",
"name":"Julio Verne Cerqueira"
},
{
"_id":{
"$oid":"60dca71f0394f430c8ca29a8"
},
"church":"Saint Antoine Hill",
"nome":"Ryan Steel Oliveira"
}
如果我尝试查找,我得到以下结果:(它正在获取整个教堂集合)。
我该怎么做查询,所以它只给了我与该成员相关的一个教会?
并且,如果可能的话,如何按教堂的字母顺序然后按名称对结果进行排序?
Obs.: MongoDB 版本: 4.4.10
$lookup
匹配错误 --> $pipeline
--> $match
.
应该是:
$match: {
$expr: {
$eq: [
"$_id",
"$$searchId"
]
}
}
根据提供的文件,members
到 churchies
的关系将是 1 到许多 。因此,当您通过 $lookup
将 members
与 churchies
连接时,输出 church
将是一个数组 只有一个 churchies
文档.
聚合管道:
$lookup
- 加入 members
collection(by $$searchId
)和 churchies
(by _id
)。
$unwind
- 将 church
数组字段解构为多个文档。
$project
- 修饰输出文档。
$sort
- 按 church
和 name
升序排序。
db.members.aggregate([
{
"$lookup": {
"from": "churchies",
"let": {
searchId: {
"$toObjectId": "$church"
}
},
"pipeline": [
{
$match: {
$expr: {
$eq: [
"$_id",
"$$searchId"
]
}
}
},
{
$project: {
name: 1
}
}
],
"as": "church"
}
},
{
"$unwind": "$church"
},
{
$project: {
_id: 1,
church: "$church.name",
name: 1
}
},
{
"$sort": {
"church": 1,
"name": 1
}
}
])
假设我有这两个集合:
// Members:
{
"_id":{
"$oid":"60dca71f0394f430c8ca296d"
},
"church":"60dbb265a75a610d90b45c6b",
"name":"Julio Verne Cerqueira"
},
{
"_id":{
"$oid":"60dca71f0394f430c8ca29a8"
},
"nome":"Ryan Steel Oliveira",
"church":"60dbb265a75a610d90b45c6c"
}
和
// Churches
{
"_id": {
"$oid": "60dbb265a75a610d90b45c6c"
},
"name": "Saint Antoine Hill",
"active": true
},
{
"_id": {
"$oid": "60dbb265a75a610d90b45c6b"
},
"name": "Jackeline Hill",
"active": true
}
我想查询它并得到这样的结果:
// Member with Church names
{
"_id":{
"$oid":"60dca71f0394f430c8ca296d"
},
"church":"Jackeline Hill",
"name":"Julio Verne Cerqueira"
},
{
"_id":{
"$oid":"60dca71f0394f430c8ca29a8"
},
"church":"Saint Antoine Hill",
"nome":"Ryan Steel Oliveira"
}
如果我尝试查找,我得到以下结果:(它正在获取整个教堂集合)。
我该怎么做查询,所以它只给了我与该成员相关的一个教会?
并且,如果可能的话,如何按教堂的字母顺序然后按名称对结果进行排序?
Obs.: MongoDB 版本: 4.4.10
$lookup
匹配错误 --> $pipeline
--> $match
.
应该是:
$match: {
$expr: {
$eq: [
"$_id",
"$$searchId"
]
}
}
根据提供的文件,members
到 churchies
的关系将是 1 到许多 。因此,当您通过 $lookup
将 members
与 churchies
连接时,输出 church
将是一个数组 只有一个 churchies
文档.
聚合管道:
$lookup
- 加入members
collection(by$$searchId
)和churchies
(by_id
)。$unwind
- 将church
数组字段解构为多个文档。$project
- 修饰输出文档。$sort
- 按church
和name
升序排序。
db.members.aggregate([
{
"$lookup": {
"from": "churchies",
"let": {
searchId: {
"$toObjectId": "$church"
}
},
"pipeline": [
{
$match: {
$expr: {
$eq: [
"$_id",
"$$searchId"
]
}
}
},
{
$project: {
name: 1
}
}
],
"as": "church"
}
},
{
"$unwind": "$church"
},
{
$project: {
_id: 1,
church: "$church.name",
name: 1
}
},
{
"$sort": {
"church": 1,
"name": 1
}
}
])