mongoose - $regex within $project mongodb aggregation -
i have users collection has location field type string. want pass score field along document shows whether document's location similar text "austin". instance recorded location austin, texas, want matched austin. thought possible use $regex this.
i wrote aggregation:
$project: { score: { $cond: if: { $regex: {'$location': /austin/} }, then: 1, else: 0 } }, location: 1, firstname: 1, lastname: 1 } but :
{ "name": "mongoerror", "errmsg": "exception: invalid operator '$regex'", "code": 15999, "ok": 0 }
use $substr instead:
{ "$project": { "score": { "$cond": [ { "$eq": [ { "$substr": [ "$location", 0, 6 ] }, "austin" ] }, 1, 0 ] }, "location": 1, "firstname": 1, "lastname": 1 } }
Comments
Post a Comment