NIX*_*Xes 6 regex grouping conditional mongodb aggregation-framework
我试图在多个阶段对数据进行分组。
目前我的查询如下所示:
db.captions.aggregate([
{$project: {
"videoId": "$videoId",
"plainText": "$plainText",
"Group1": {$cond: {if: {$eq: ["plainText", {"$regex": /leave\sa\scomment/i}]},
then: "Yes", else: "No"}}}}
])
Run Code Online (Sandbox Code Playgroud)
我不确定是否真的可以在聚合阶段的 $cond 中使用 $regex 运算符。我非常感谢您的帮助!
提前致谢
更新:从 MongoDB v4.1.11 开始,您的问题似乎终于有了一个很好的解决方案,记录在此处。
原答案:
正如我在上面的评论中所写的那样,目前$regex在内部不起作用$cond。有一个开放的JIRA 票,但它是,呃,好吧,开放......
在您的具体情况下,我倾向于建议您在客户端解决该主题,除非您正在处理大量输入数据,而这些输入数据将始终只返回小子集。从您的查询来看,您似乎总是要检索刚刚分入两个结果组(“是”和“否”)的所有文档。
如果您不想或无法在客户端解决该主题,那么这里有一些使用$facet(需要 MongoDB >= v3.4)的东西 - 它既不是特别快也不是特别漂亮,但它可能会帮助您入门。
db.captions.aggregate([{
$facet: { // create two stages that will be processed using the full input data set from the "captions" collection
"CallToActionYes": [{ // the first stage will...
$match: { // only contain documents...
"plainText": /leave\sa\scomment/i // that are allowed by the $regex filter (which could be extended with multiple $or expressions or changed to $in/$nin which accept regular expressions, too)
}
}, {
$addFields: { // for all matching documents...
"CallToAction": "Yes" // we create a new field called "CallsToAction" which will be set to "Yes"
}
}],
"CallToActionNo": [{ // similar as above except we're doing the inverse filter using $not
$match: {
"plainText": { $not: /leave\sa\scomment/i }
}
}, {
$addFields: {
"CallToAction": "No" // and, of course, we set the field to "No"
}
}]
}
}, {
$project: { // we got two arrays of result documents out of the previous stage
"allDocuments" : { $setUnion: [ "$CallToActionYes", "$CallToActionNo" ] } // so let's merge them into a single one called "allDocuments"
}
}, {
$unwind: "$allDocuments" // flatten the "allDocuments" result array
}, {
$replaceRoot: { // restore the original document structure by moving everything inside "allDocuments" up to the top
newRoot: "$allDocuments"
}
}, {
$project: { // include only the two relevant fields in the output (and the _id)
"videoId": 1,
"CallToAction": 1
}
}])
Run Code Online (Sandbox Code Playgroud)
与聚合框架一样,它可能有助于从管道末端删除单个阶段并运行部分查询,以便了解每个阶段的作用。
| 归档时间: |
|
| 查看次数: |
4797 次 |
| 最近记录: |