在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