En primer lugar, es all_category_id
, no category_id
. En segundo lugar, no vincula artículos:todos los documentos tendrán exactamente la misma article_category
formación. Por último, probablemente desee filtrar los artículos que no tienen una categoría coincidente. La canalización condicional debería parecerse más a esto:
db.article.aggregate([
{ $match: {
title: { $regex: /example/ }
} },
{ $lookup: {
from: "article_category",
let: {
article_id: "$article_id"
},
pipeline: [
{ $match: {
$expr: { $and: [
{ $in: [ 8, "$all_category_id" ] },
{ $eq: [ "$article_id", "$$article_id" ] }
] }
} }
],
as: "article_category"
} },
{ $match: {
$expr: { $gt: [
{ $size: "$article_category"},
0
] }
} }
] )
ACTUALIZACIÓN:
Si no coincide con article_id
, el $lookup
dará como resultado article_category
idénticos matriz a todos los artículos.
Digamos su article_category
colección tiene otro documento:
{
"article_id": 0,
"all_category_id": [5,8,10]
}
Con { $eq: [ "$article_id", "$$article_id" ] }
en la canalización el article_category
resultante es
[
{
"article_id" : 2015110920343902,
"all_category_id" : [ 5, 8, 10 ]
}
]
sin:
[
{
"article_id" : 2015110920343902,
"all_category_id" : [ 5, 8, 10 ]
},
{
"article_id": 0,
"all_category_id": [ 5, 8, 10 ]
}
]
Si lo último es lo que necesita, sería mucho más sencillo hacer para encontrar solicitudes:
db.article.find({ title: { $regex: /example/ } })
y
db.article_category.find({ all_category_id: 8 })