0

I'm trying to do a 'like' query in mongodb. I see that's done with a regexp so I'm trying to set it like this:

$scope.clients = Client.query({
        q:angular.toJson({
            name: RegExp($routeParams.str)
        })
    });

The thing is angular.toJson function does not get any regexp: http://plnkr.co/edit/idXMT1

Is there any other way to do that?

subarroca
  • 851
  • 7
  • 22

2 Answers2

0

From my understanding of your question, you want your JSON object to contain the regular expression as a string?

In that case, you can manually convert it to a string in the declaration, i.e:

$scope.object = angular.toJson({
    "param" : new RegExp(str).toString()
});

See updated plunk:

http://plnkr.co/edit/yLogI8

Alex Osborn
  • 9,831
  • 3
  • 33
  • 44
  • Well, it's not that easy, I want to pass an object like suggested in here: http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like but with your solution I finally pass a string and the search returns no results – subarroca Feb 24 '13 at 10:04
0

I finally found a solution.

The problem is using the toJon function as it does not allow any key starting with / or $ so the solution is avoiding the use of this function and just write the object as a string

$scope.clients = Client.query({
    q:'{src:{$regex:"' + $routeParams.str + '"}}'
};
subarroca
  • 851
  • 7
  • 22