Find matching partial strings

I can not find the answer on google, as well as on mongoose, so I ask here. If possible, how can I find the partially matching line in the document.

ex:

In the user collection:

{ name: "Louis", location: "Paris, France" },
{ name: "Bill", location: "Paris, Illinoid" },
{ name: "Stephen", location: "Toronto, Ontario" }

Mongoose Function:

searchForCity("Paris");

The result will be a list of documents from the collection of users having “Paris” in the location String. eg:

[
    { name: "Louis", location: "Paris, France" },
    { name: "Homer", location: "Paris, Illinois" }
]
+5
source share
1 answer

You can use regex for this:

Query#regex, Query#$regex

Defines an operator $regex.

query.regex('name.first', /^a/i)

So something like this:

User.where('location').$regex(/Paris/);
User.where('location').$regex(/paris/i); // Case insensitive version

, , MongoDB location. :

User.where('location').$regex(/^Paris/);

, MongoDB .

+16

All Articles