You can try lookup with aggregation pipeline,
$lookup
in branches collection, pass productId
in let
, and match expression condition for for productId
and filter userId
db.products.aggregate([
{
$lookup: {
from: "branches",
let: { productId: "$_id" },
pipeline: [
{
$match: {
userId: "1", // filter user id here
$expr: { $eq: ["$$productId", "$productId"] }
}
}
],
as: "brnaches"
}
}
])
Playground
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…