The Query.prototype.collation() allows users to specify language-specific rules for various comparison like string comparison such as rules for accent marks or rules for letter case, etc.
Syntax:
Query.prototype.collation()
Parameters: This function has one array parameter i.e. an object which can have various properties like locale, caseLevel, caseFirst, etc.
Return Value: This function returns Query Object.
Installing mongoose :
npm install mongoose
After installing the mongoose module, you can check your mongoose version in command prompt using the command.
npm mongoose --version
After that, you can just create a folder and add a file for example, index.js as shown below.
Example 1:
index.js
const mongoose = require( 'mongoose' );
useNewUrlParser: true ,
useCreateIndex: true ,
useUnifiedTopology: true
});
const User = mongoose.model( 'User' , {
name: { type: String },
age: { type: Number }
});
var query = User.find();
query.collation({ caseFirst: "upper" })
console.log( "Collation set to : " , query.options)
|
The project structure will look like this:

Run index.js file using below command:
node index.js
Output:
Collation set to : { collation: { caseFirst: 'upper' } }
Example 2:
index.js
const express = require( 'express' );
const mongoose = require( 'mongoose' );
const app = express()
useNewUrlParser: true ,
useCreateIndex: true ,
useUnifiedTopology: true
});
const User = mongoose.model( 'User' , {
name: { type: String },
age: { type: Number }
});
var query = User.find();
query.collation({ caseFirst: "upper" })
console.log( "Collation : " , query.options)
app.listen(3000, function (error ){
if (error) console.log(error)
console.log( "Server listening on PORT 3000" )
})
|
The project structure will look like this:

Run index.js file using below command:
node index.js
Output:
Server listening on PORT 3000
Collation : { collation: { caseFirst: 'upper' } }
Reference:https://mongoosejs.com/docs/api/query.html#query_Query-collation
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Mar, 2021
Like Article
Save Article