在MongoDB中,我想根据某个字段是否具有特定子字符串来对文档进行分组。我试图将每个文档投影到一个布尔值,说明该字段是否具有该子字符串/匹配该模式,然后按该字段进行分组。在MongoDB中,我可以预测一个值是否有一个子字符串到一个新的字段?
我该怎么做?
编辑:
1)我已经尝试了聚合管道作为
db.aggregate([
{
'$match': {
'$regex': '.*world'
}
}
])
但我得到一个错误:
pymongo.errors.OperationFailure: command SON([('aggregate', u'test'), ('pipeline', [{'$match': {'$regex': '.*world'}}])]) failed: exception: bad query: BadValue unknown top level operator: $regex
2)我不是只选择单词符合模式。我想选择每个文件。我想创建一个名为“匹配”的字段,它表示模式是否匹配每个元素。
你在问关于MongoDB [Aggregation Pipeline](http://docs.mongodb.org/manual/core/aggregation-pipeline/)吗? – 2014-09-13 16:24:01
你可以聚合你的收藏文件,并找出哪些与我编辑过的问题匹配给定的''regex'' – GuillaumeA 2014-09-14 13:58:37
。 – 2014-09-14 17:10:07