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
看来,我试图使用正则表达式的方式是不正确的或在不正确的地方/操作。
谢谢!我看到了一个,但没有得到任何工作,所以我想专注于$正则表达式,但我发现它不会以我想要使用它的方式工作。 –