I have a collection called article_category
which store all article_id
belongs to the category with category_id
with data format like so.
Collection 1: article_category
{
"article_id": 2015110920343902,
"all_category_id": [5,8,10]
}
Then I have other collection called article
which store all my post
Collection 2: article
{
"title": "This is example rows in article collection"
"article_id": 2015110920343902,
},
{
"title": "Something change"
"article_id": 2015110920343903,
},
{
"title": "This is another rows",
"article_id": 2015110920343904,
}
Now I want to perform MongoDB query to find title
with regex
while category_id
must equal to 8
. Here is my query but is not work.
db.article.aggregate(
{
$match:
{
title:
{
$regex: /example/
}
}
},
{
$lookup:
{
from: "article_category",
pipeline: [
{ $match: { category_id: 8 } }
],
as: "article_category"
}
}
)
Above query only show the records which match by regex
but not match by category_id
.
Any idea?