对 Mongodb 嵌套聚合有疑问
Have a Question about Mongodb Aggregate if nested
我正在使用 aggregate 创建一个函数。我正在使用查找绑定三个数据库。下面这个函数组成的时候,有没有办法绑定A的数据和C的数据?
A.aggregate([
{
$lookup: {
from: "B",
let: { aid: "$aid" },
pipeline: [
{ $match: { $expr: { $in: ["$_id", "$$aid"] } } },
{
$lookup: {
from: "C",
let: { bid: "$bid" },
pipeline: [
{
$match: {
$expr: {
$eq: ["$$bid, "$aid"],
},
},
},
],
as: "list",
},
},
...
])
我对此有了更多的了解,并且通过执行以下操作找到了我正在寻找的结果:
A.aggregate([
{
$lookup: {
from: "B",
let: { bid: "$bid", aid: "$aid" },
pipeline: [
{ $match: { $expr: { $in: ["$_id", "$$bid"] } } },
{
$lookup: {
from: "C",
localField: "_id",
foreignField: "cid",
as: "list",
},
},
{
$unwind: "$list",
},
// One More Match
{
$match: { $expr: { $eq: ["$$aid", "$list.aid"] } },
},
{
$addFields: {
list: "$list.clist",
},
},
],
as: "blist",
},
},
])
我正在使用 aggregate 创建一个函数。我正在使用查找绑定三个数据库。下面这个函数组成的时候,有没有办法绑定A的数据和C的数据?
A.aggregate([
{
$lookup: {
from: "B",
let: { aid: "$aid" },
pipeline: [
{ $match: { $expr: { $in: ["$_id", "$$aid"] } } },
{
$lookup: {
from: "C",
let: { bid: "$bid" },
pipeline: [
{
$match: {
$expr: {
$eq: ["$$bid, "$aid"],
},
},
},
],
as: "list",
},
},
...
])
我对此有了更多的了解,并且通过执行以下操作找到了我正在寻找的结果:
A.aggregate([
{
$lookup: {
from: "B",
let: { bid: "$bid", aid: "$aid" },
pipeline: [
{ $match: { $expr: { $in: ["$_id", "$$bid"] } } },
{
$lookup: {
from: "C",
localField: "_id",
foreignField: "cid",
as: "list",
},
},
{
$unwind: "$list",
},
// One More Match
{
$match: { $expr: { $eq: ["$$aid", "$list.aid"] } },
},
{
$addFields: {
list: "$list.clist",
},
},
],
as: "blist",
},
},
])