Node.js is a popular runtime platform to create programs that run on it.
It lets us run JavaScript outside the browser.
In this article, we’ll look at how to start using Node.js to create programs.
Collations
Collations are sets of sorting rules that are used when we do string order for specific languages and locales.
We can specify the collation property when we create the collection.
For example, we can write:
const { MongoClient } = require('mongodb');
const connection = "mongodb://localhost:27017";
const client = new MongoClient(connection);
async function run() {
try {
await client.connect();
const db = client.db("test");
await db.dropCollection('test');
await db.createCollection("test", {
collation: { locale: "en" },
});
const testCollection = await db.collection('test');
await testCollection.dropIndexes();
await testCollection.deleteMany({})
const result = await testCollection.insertMany([
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
]);
console.log(result)
const query = {};
const projection = { name: 1 };
const cursor = testCollection
.find(query)
.project(projection);
cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);
We call createCollection with the collection name as the first argument.
The 2nd argument is an object where we have the collation property to set the collation rule.
Then we call find to query the collection.
Assign a Collation to an Index
We can also assign a collection to an index.
For example, we can write:
const { MongoClient } = require('mongodb');
const connection = "mongodb://localhost:27017";
const client = new MongoClient(connection);
async function run() {
try {
await client.connect();
const db = client.db("test");
await db.dropCollection('test');
await db.createCollection("test");
const testCollection = await db.collection('test');
await testCollection.createIndex(
{ 'name': 1 },
{ 'collation': { 'locale': 'en' } });
await testCollection.dropIndexes();
await testCollection.deleteMany({})
const result = await testCollection.insertMany([
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
]);
console.log(result)
const query = { name: 'apple' };
const options = { "collation": { "locale": "en_US" } };
const cursor = testCollection
.find(query, options)
cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);
We have:
await testCollection.createIndex(
{ 'name': 1 },
{ 'collation': { 'locale': 'en' } });
We called createIndex with the fields to index in the first argument.
The 2nd argument has the collation options.
Then we can use it by writing:
const { MongoClient } = require('mongodb');
const connection = "mongodb://localhost:27017";
const client = new MongoClient(connection);
async function run() {
try {
await client.connect();
const db = client.db("test");
await db.dropCollection('test');
await db.createCollection("test");
const testCollection = await db.collection('test');
await testCollection.createIndex(
{ 'name': 1 },
{ 'collation': { 'locale': 'en' } });
await testCollection.dropIndexes();
await testCollection.deleteMany({})
const result = await testCollection.insertMany([
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
]);
console.log(result)
const cursor = testCollection
.find()
.sort({ "name": -1 });
cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);
We sort the name field with the sort method in descending order.
Conclusion
We can add collations applying sorting rules for a specific language with MongoDB.