Member-only story
Node.js Basics — MongoDB Comparison Operators
3 min readJan 19, 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.
Comparison Operators
We can use comparison operators to query for items with one or more quantities bigger than or less than some quantity.
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 testCollection = await client.db("test").collection('test');
const result = await testCollection.insertMany([
{
name: "Popeye",
rating: 5
},
{
name: "KFC",
rating: 4
},
]);
console.log(result)
const query = { rating: { $gt: 4 } };
const cursor = testCollection.find(query);
await cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);
The $gt
key means greater than, so we search for the documents that have rating
bigger than 4.