2013-07-31 50 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

谢谢!我看到了一个,但没有得到任何工作,所以我想专注于$正则表达式,但我发现它不会以我想要使用它的方式工作。 –