简体   繁体   中英

How to query MongoDB over HTTP API with “like”?

I want to query data from MongoDB via the HTTP API in AngularJS. My code is like this:

$http.get('api/data', {user: user1});

How I can I change it, so that is searches for %user% like in SQL the LIKE operator? This both lines does not work:

$http.get('api/data', {user: /user1/});
$http.get('api/data', {user: {'$regex': 'user1'}});

It does not depend on your webservice call. You are already sending regex. You need to use it on your server, while finding the record in mongodb.

MongoDB has a builtin regex search: https://docs.mongodb.com/manual/reference/operator/query/regex/

This should work for you:

db.users.find( { user: { $regex: your_regex_here } } )

Checkout the mongodb docs (from above link) for more options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM