Mongoose SchemaType.prototype.immutable() API
Mongoose is a MongoDB object modeling and handling for a node.js environment.
Mongoose SchemaType immutable property sets a mongoose schema path as immutable, that is, it does not allow you to change the path value unless the isNew property for the schema type is set to true. Let’s understand more about this with some examples.
Syntax:
mongoose.schema({ [name]: { type: [type], immmutable: Boolean (true | false) } })
Parameters: It accepts a boolean value as a parameter.
Return type: It returns a SchemaType as the 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 ReactJS 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 create a getter an immutable property name, and try to modify its value by directly assigning it to a different name.
Filename: main.js
Javascript
//Importing the module const mongoose = require( 'mongoose' ) // Connecting to the database 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, immutable: true } }); const Person = mongoose.model( 'Person' , personSchema); (async () => { await Person.create({ name: 'John' }); const person = await Person.findOne({ name: 'John' }); console.log(person.isNew); person.name = 'new name' ; console.log(person.name); })() |
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 create a getter an immutable property name, and try to modify its value by using updateOne helper method of mongoose Schema API.
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, immutable: true } }); const Person = mongoose.model( 'Person' , personSchema); (async () => { await Person.create({ name: 'John' }); Person.updateOne({ name: 'John' }, { name: 'Doe' }, { strict: 'throw' }) .then(() => null , err => err) const persons = await Person.find(); console.log(persons); })() |
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-immutable
Contact Us