我正在尝试创建一个管道,它将计算有多少文档符合某些条件.我看不出在条件中使用正则表达式的任何方法.这是带有注释的管道的简化版本:
db.Collection.aggregate([
// Pipeline before the issue
{'$group': {
'_id': {
'field': '$my_field', // Included for completeness
},
'first_count': {'$sum': { // We're going to count the number
'$cond': [ // of documents that have 'foo' in
{'$eq: ['$field_foo', 'foo']}, 1, 0 // $field_foo.
]
}},
'second_count': {'$sum': { // Here, I want to count the
'$cond': [ // Number of documents where
{'$regex': ['$field_bar', regex]}, 1, 0 // the value of 'bar' matches
] // the regex
}},
}, …Run Code Online (Sandbox Code Playgroud)