regex - Mongodb 在带有正则表达式查询的数组字段上不同?

标签 regex mongodb mongodb-query aggregation-framework

基本上我正在尝试在模型上实现标签功能。

> db.event.distinct("tags")
[ "bar", "foo", "foobar" ]

执行一个简单的不同查询会检索所有不同的标签。但是,我将如何获取与某个查询匹配的所有不同标签?举例来说,我想获取与 foo 匹配的所有标签,然后期望得到 ["foo","foobar"] 结果?

以下查询是我实现此目标的失败尝试:

> db.event.distinct("tags",/foo/)
[ "bar", "foo", "foobar" ]

> db.event.distinct("tags",{tags: {$regex: 'foo'}})
[ "bar", "foo", "foobar" ]

最佳答案

aggregation framework而不是 .distinct() 命令:

db.event.aggregate([
    // De-normalize the array content to separate documents
    { "$unwind": "$tags" },

    // Filter the de-normalized content to remove non-matches
    { "$match": { "tags": /foo/ } },

    // Group the "like" terms as the "key"
    { "$group": {
        "_id": "$tags"
    }}
])

您可能最好在正则表达式的开头使用“ anchor ”,即从字符串的“开始”开始。并且还这样做$match在处理 $unwind 之前还有:

db.event.aggregate([
    // Match the possible documents. Always the best approach
    { "$match": { "tags": /^foo/ } },

    // De-normalize the array content to separate documents
    { "$unwind": "$tags" },

    // Now "filter" the content to actual matches
    { "$match": { "tags": /^foo/ } },

    // Group the "like" terms as the "key"
    { "$group": {
        "_id": "$tags"
    }}
])

这确保您没有处理 $unwind在您“过滤”之前,仅对集合中的每个文档以及可能包含“匹配标签”值的文档进行确认。

真正“复杂”的方法在某种程度上缓解可能匹配的大型数组需要更多的工作,并且 MongoDB 2.6 或更高版本:

db.event.aggregate([
    { "$match": { "tags": /^foo/ } },
    { "$project": {
        "tags": { "$setDifference": [
            { "$map": {
                "input": "$tags",
                "as": "el",
                "in": { "$cond": [
                    { "$eq": [ 
                        { "$substr": [ "$$el", 0, 3 ] },
                        "foo"
                    ]},
                    "$$el",
                    false
                ]}
            }},
            [false]
        ]}
    }},
    { "$unwind": "$tags" },
    { "$group": { "_id": "$tags" }}
])

所以$map是一个很好的“内联”数组处理器,但它只能走这么远。 $setDifference运算符否定 false 匹配,但最终您仍然需要处理 $unwind 来完成剩余的 $group 阶段,以获得总体上不同的值。

这里的优点是数组现在“减少”为仅匹配的“标签”元素。当您想要对同一文档中存在“多个不同”值的出现次数进行“计数”时,请不要使用此选项。但同样,还有其他方法可以解决这个问题。

关于regex - Mongodb 在带有正则表达式查询的数组字段上不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28090205/

相关文章:

python - 如何在 django 中为继承的模型属性创建通用模型表单字段验证器?

c++ - 如何使用正则表达式中的 sscanf() 解析字符串?

mongodb - Ubuntu 16.04 - MongoDB 3.4.x - 安装特定版本

javascript - 使用 $regex 查询 Mongoose 不返回任何结果

mongodb - 将集合中的所有文档从 mongodb 移动到 azure blob 存储

Javascript 正则表达式修饰符

regex - (相当于) "backreferences within character class"的一般方法?

javascript - 无法通过 'findById' 和 save() 更新文档

mongodb - 按 ObjectID 分片,是正确的方法吗?

javascript - 从 MongoDB 中删除具有特定键数的文档