我正在尝试检查字段的第一个字符不是 az
db.zips.aggregate(
[
{ $group : { city: { $regex: '^[a-z]', $options: 'i' }, n : {$sum:1} } }
]
)
这是一个示例文档,预计会找到,但查询错误与未知运算符“$ regex”有关。安装了 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
谢谢!
PS我也试过这个:
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
因此,我尝试使用正则表达式的方式似乎不正确或在不正确的位置/操作中。