$regex within $project mongodb aggregation

微笑、不失礼 提交于 2020-02-03 13:19:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!