基本上我正在尝试在模型上实现标签功能。
> 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/