Mongoose Partial Text Search

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Before I show you how to do a partial text search in code let’s discuss a real-world example. We’re going to give you a real-life example from the web development industry. If you have visited any kind of the e-commerce like Amazon or AirBnb. If you use the search form and start typing then it will automatically start suggesting related search terms. This is one great example of partial text search being used in websites.

Prerequisites

  • You should be familiar with Javascript.
  • You should have NodeJS installed. We will run our Javascript using NodeJS.
  • You’ll want to be familiar with Mongoose. For a quick recap it is a third-party library that lets you interact with MongoDB through mdoels.
  • You don’t have to know regular expressions but once you get the basics of how to do a partial text search, knowing regular expressions will come in extremely useful.

So without wasting let’s implement the code for a Mongoose partial text search.

How to Implement Partial Text Search using Mongoose?

I’m going to use Regular expression(regex) to implement the partial text search in Mongoose. You can use another approach with match, in, or by using other operators.

File: mongoosemodel.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var mongoose = require("mongoose");
var userProfile = new mongoose.Schema({
name: { type: String },
age: { type: Number }
});

module.exports = mongoose.model("userProfile", userProfile);
var newUser = mongoose.model("userProfile", userProfile);
const allusers = newUser.find({}, "name age", function(err, docs) {
if (err) console.log(err);
console.log(docs);
});


newUser.find({ name: { $regex: "s", $options: "i" } }, function(err, docs) {
console.log("Partial Search Begins");
console.log(docs);
});

In the code snippet, the first find() function that I have used is to showcase all the data present inside the database. 

1
2
3
4
const allusers = newUser.find({}, "name age", function(err, docs) {
if (err) console.log(err);
console.log(docs);
});

After that, I have used another find() function in the same document but this time I have passed a value to regex. This passes the value from find function and it will search for all the values that start with our text we have put inside the regex. In this specific case we are searching for names that start with the letter ‘s’. The options allow us to do a case-insensitive search ( upper and lower case ‘s’ ‘S’ will both match ).

1
2
3
4
newUser.find({ name: { $regex: "s", $options: "i" } }, function(err, docs) {
console.log("Partial Search Begins");
console.log(docs);
});

Once you add that code just run that code using “npm start”. This will give you the result as shown above or below:

Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\mongooseproject>npm start
> mongooseproject@1.0.0 start C:\mongooseproject
> node index.js
*** MongoDB got connected ***
Our Current Database Name : mongoosepractice
[
{ _id: 5d49c275e0957b0f70cb91ad, name: 'Eva', age: 21 },
{ _id: 5d49c441b628860f707ad52f, name: 'Sam', age: 23 },
{ _id: 5d49c471b628860f707ad530, name: 'Adam', age: 48 },
{ _id: 5d49c666b628860f707ad531, name: 'Raymond', age: 20 },
{ _id: 5d4b3ca636e6bd2b4c0bf705, name: 'Steve', age: 22 } ]
Partial Search Begins
[
{ _id: 5d49c441b628860f707ad52f, name: 'Sam', age: 23 },
{ _id: 5d4b3ca636e6bd2b4c0bf705, name: 'Steve', age: 22 } ]

Conclusion

This has been a very simple example of how to implement a partial text search in Mongoose. We utilized regular expressions to do the text matching. Becoming familiar with regular expressions will be very handy.

We hope you were able to apply the code snippets here to your specific situation. Thanks for reading!

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.