2013-07-31 47 views
0

我試圖檢查一個字段的第一個字符不爲AZ

db.zips.aggregate( 
[ 
{ $group : { city: { $regex: '^[a-z]', $options: 'i' }, n : {$sum:1} } } 
] 
) 

這裏是預期發現的樣本文檔,但未知運營商查詢錯誤「 $正則表達式」。 MongoDB的v2.4.5安裝:

{"city": "25536", "loc": [-82.076761, 38.087553], "pop": 61, "state": "WV", "_id": "25536"} 

錯誤:

Error: Printing Stack Trace 
at printStackTrace (src/mongo/shell/utils.js:37:15) 
at DBCollection.aggregate (src/mongo/shell/collection.js:897:9) 
at (shell):1:9 
Wed Jul 31 12:14:14.737 JavaScript execution failed: aggregate failed: { 
    "errmsg" : "exception: unknown group operator '$regex'", 
    "code" : 15952, 
    "ok" : 0 
} at src/mongo/shell/collection.js:L898 

謝謝!

P.S.我也試過這個:

db.zips.aggregate( 
[ 
{ $project : { city: /^[a-z]/i }} 
,{ $group : { city : "$city", n : {$sum:1} } } 
] 
) 

Error: Printing Stack Trace 
at printStackTrace (src/mongo/shell/utils.js:37:15) 
at DBCollection.aggregate (src/mongo/shell/collection.js:897:9) 
at (shell):1:9 
Wed Jul 31 12:26:21.531 JavaScript execution failed: aggregate failed: { 
    "errmsg" : "exception: disallowed field type RegEx in object expression 
(at 'city')", 
    "code" : 15992, 
    "ok" : 0 
} at src/mongo/shell/collection.js:L898 

看來,我試圖使用正則表達式的方式是不正確的或在不正確的地方/操作。

回答

2

$正則表達式不能有使用,你想看JIRA這樣的:https://jira.mongodb.org/browse/SERVER-8892

不過,我想你實際上是由正則表達式,而不是組要$match。所以你想在你的標準中匹配所有的城市,然後你想分組他們。

作爲參考這裏是一個重複的問題,我只是谷歌搜索發現:using $regex in mongodb aggregation framework in $group

+0

謝謝!我看到了一個,但沒有得到任何工作,所以我想專注於$正則表達式,但我發現它不會以我想要使用它的方式工作。 –