在聚合框架mongodb中使用$ unwind和$ text

时间:2016-10-06 13:34:21

标签: mongodb mongodb-query aggregation-framework

我在mongo db中有一个集合称为调用页面。 在那里我有一系列文章称为文章。 在该数组中的每个文档中,我都说了文章编号和文章内容。

我想要做的是解开文章,然后使用$ text在文章内容中搜索单词。但$ text必须处于管道的第一阶段。

如果我在没有展开的情况下执行第一阶段的管道,那么现在会发生什么呢?在第一次搜索文本时,它会返回该文档的所有剩余文章,而不管它是否有文本。

注意:Pages集合包含大量文档。

样品采集:

{
   pageNo: 1,
   articles:[{
          articleNo:1,
          articleContent:"cat dog cat dog"
        },{
          articleNo:2,
          articleContent:" Some random text"
        }]
},
{
   pageNo: 2,
   articles:[{
          articleNo:1,
          articleContent:"Some random text"
        },{
          articleNo:2,
          articleContent:"cat dog cat"
        }]
}

预期输出:假设我搜索" cat"

{
   pageNo:1,
    articles:[{
          articleNo:1,
          articleContent:"cat dog cat dog"
        }]
},
{
  pageNo:2,
   articles:[{
          articleNo:2,
          articleContent:"cat dog cat" 
        }]
}

1 个答案:

答案 0 :(得分:1)

以下答案将返回您想要的结果。在$match索引的帮助下,第一个cat仅用于过滤其中没有text的文档。如果你不使用这个阶段,结果将是相同和正确的,但可能会更慢。

db.pages.aggregate([
     {
         $match: {
             $text: {
                 $search: "cat"
             }
         } 
     },
     {
         $unwind: '$articles'
     },
     {
         $match: {
             'articles.articleContent': /cat/
         }
     },
     {
         $group: {
             _id: {
                 _id: '$_id',
                 pageNo: '$pageNo'
             },
             articles: {
                 $push: '$articles'
             }
         }
     },
     {
         $project: {
             _id: '$_id._id',
             pageNo: '$_id.pageNo',
             articles: 1
         }
     }
])