问题
I have a users
collection which has a location field with type String
. I want to pass a score field along the document which shows whether the document's location is similar to the text "Austin". For instance the recorded location is Austin, Texas, I want it to be matched with Austin. I thought it would be possible to use $regex for this.
I wrote this aggregation:
$project: {
score: {
$cond:
if: {
$regex: {'$location': /Austin/}
},
then: 1,
else: 0
}
},
location: 1,
firstName: 1,
lastName: 1
}
But what I get is :
{
"name": "MongoError",
"errmsg": "exception: invalid operator '$regex'",
"code": 15999,
"ok": 0
}
来源:https://stackoverflow.com/questions/29866336/regex-within-project-mongodb-aggregation