Member-only story
Node.js Basics — MongoDB Collation Rules for Deletion and Aggregation
2 min readJan 21, 2021
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.
Collation and findOneAndDelete
We can set collation rules with the findOneAndDelete
method.
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 },
])…