【发布时间】:2018-07-09 03:35:04
【问题描述】:
我有一个名为article_category 的集合,它存储所有article_id 属于category_id 的类别,数据格式如下。
集合 1:article_category
{
"article_id": 2015110920343902,
"all_category_id": [5,8,10]
}
然后我有另一个名为 article 的集合,其中存储了我所有的帖子
集合 2:文章
{
"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,
}
现在我想执行 MongoDB 查询以找到 title 和 regex,而 category_id 必须等于 8。这是我的查询,但不起作用。
db.article.aggregate(
{
$match:
{
title:
{
$regex: /example/
}
}
},
{
$lookup:
{
from: "article_category",
pipeline: [
{ $match: { category_id: 8 } }
],
as: "article_category"
}
}
)
以上查询只显示regex匹配但category_id不匹配的记录。
有什么想法吗?
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework