Mongoose SchemaType.prototype.text() API
Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document.
The Mongoose SchemaType API text method is used to declare a full-text index depending on the parameter passed to the method. Let’s understand more about this with some examples.
Syntax:
SchemaType.prototype.text()
Parameters: It takes a boolean parameter as input.
Return type: It returns a SchemaType Object as a response.
Creating node application And Installing Mongoose:
Step 1: Create a node application using the following command:
mkdir folder_name cd folder_name npm init -y touch main.js
Step 2: After creating the application, Install the required module using the following command:
npm install mongoose
Project Structure: It will look like the following.
GUI Representation of the Database using MongoDB Compass: Currently, the collection has no data.
Example 1: In this example, we will use the SchemaType API text() method to create a text index on the “name” path by assigning the “text” attribute In the schema definition itself.
Filename: main.js
Javascript
const mongoose = require( 'mongoose' ) // Database connection mongoose.connect( 'mongodb://localhost:27017/query-helpers' , { dbName: 'event_db' , useNewUrlParser: true , useUnifiedTopology: true }, err => err ? console.log(err) : console.log ( 'Connected to database' )); const personSchema = new mongoose.Schema({ name: { type: String, text: true }, age: { type: Number, } }); const personsArray = [ { name: 'Luffy' , age: 22 } ] const Person = mongoose.model( 'Person' , personSchema); |
Step to Run Application: Run the application using the following command from the root directory of the project:
node main.js
Output: We see that the value remains unchanged in the result.
GUI Representation of the Database using MongoDB Compass:
Example 2: In this example, we will use the SchemaType API text() method to create a text index on the “name” path by calling the “index” method on the “name” path explicitly.
Filename: main.js
Javascript
const mongoose = require( 'mongoose' ) // Database connection mongoose.connect( 'mongodb://localhost:27017/query-helpers' , { dbName: 'event_db' , useNewUrlParser: true , useUnifiedTopology: true }, err => err ? console.log(err) : console.log( 'Connected to database' )); const personSchema = new mongoose.Schema({ name: { type: String, }, age: { type: Number, } }); const personsArray = [ { name: 'Luffy' , age: 22 } ] const Person = mongoose.model( 'Person' , personSchema); personSchema.path( 'name' ).index({ text: true }) |
Step to Run Application: Run the application using the following command from the root directory of the project:
node main.js
Output: We see that the value remains unchanged in the result.
GUI Representation of the Database using MongoDB Compass:
Reference: https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-text