Categories
MongoDB Node.js Basics

Node.js Basics — MongoDB Cursor and Sorting

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.

Closing the Cursor

To clean up any resources used after getting the results from a collection, we call the cursor.close method.

To do that, we 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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
    };
    const cursor = testCollection.find(query);
    const queryResult = await cursor.toArray();
    console.log(queryResult);
    await cursor.close();
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We call cursor.close after we’re done using the cursor.

Sort Results

The MongoDB client lets us sort the results we retrieve.

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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {};
    const sort = { rating: -1 };
    const cursor = testCollection.find(query).sort(sort);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We sort the results by the rating property in descending order.

-1 means we’re sorting by descending order.

Also, we can sort by multiple fields. 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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {};
    const sort = { rating: 1, qty: 1 };
    const cursor = testCollection.find(query).sort(sort);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

Skip Returned Results

We can skip returned results. To do that, we call the find method a collection object with the 2nd argument that has the skip property.

To do that, we 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');
    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 options = {
      sort: { rating: -1 },
      skip: 2,
    };
    const cursor = testCollection.find(query, options);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

The options object has the sort property to sort the items by the rating property in descending order.

The skip property is set to the number of items to skip.

Conclusion

We can sort and skip results from with the cursor object.

Also, we should close the cursor when we’re done working with it.

Categories
MongoDB Node.js Basics

Node.js Basics — MongoDB — Ways to get Query Results

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.

Get Results with Asynchronous Iteration

We can get results with async iteration.

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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
      qty: {
        $gte: 90,
        $lt: 110,
      },
    };
    const cursor = testCollection.find(query);
    for await (const doc of cursor) {
      console.log(doc);
    }
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We get the data with a query.

Then we can loop through the entries wirh the for-await-of loop to loop through the entries.

Manual Iteration of Results

Also, we can manually iterate through the results.

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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
      qty: {
        $gte: 90,
        $lt: 110,
      },
    };
    const cursor = testCollection.find(query);
    while (await cursor.hasNext()) {
      console.log(await cursor.next());
    }
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

to add a while loop that calls the hasNext method that returns a promise that indicates whether there are any results with the resolved value.

Then the cursor.next method returns a promise with the resolved item.

Getting the Results with Stream API

We can pipe the results to a write stream by calling the cursor.pipe method.

For example, we can write:

const { MongoClient } = require('mongodb');
const { Writable } = require('stream');
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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
      qty: {
        $gte: 90,
        $lt: 110,
      },
    };
    const cursor = testCollection.find(query);
    cursor.pipe(
      new Writable({
        objectMode: true,
        write(doc, _, callback) {
          console.log(doc);
          callback();
        },
      }),
    );
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We pass an object into the Writable constructor to pipe the items with the write method.

The write method has the doc parameter with the document.

callback is called to indicate that we’re done streaming the result.

Conclusion

There are many ways to get results from the returned results with the MongoDB Node.js client.

Categories
MongoDB Node.js Basics

Node.js Basics — MongoDB Cursor

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.

Event API

We can get query results from our collection with Node.js’s Event API.

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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
    };
    const cursor = testCollection.find(query);
    cursor.on("data", data => console.log(data));
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

to watch for the 'data' event so that we can get the data from the result.

Fetch Results as an Array

We can ferch results as an array.

For instane, 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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
    };
    const cursor = testCollection.find(query);
    const allValues = await cursor.toArray();
    console.log(allValues);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We call the toArray method on the cursor which returns a promise that resolves to the result from test collection.

Cursor Utility Methods

The cursor object has a few utility methods.

The count method returns the number of results retrieved from a 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 testCollection = await client.db("test").collection('test');
    const result = await testCollection.insertMany([
      {
        name: "Popeye",
        rating: 5,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
    };
    const cursor = testCollection.find(query);
    const count = await cursor.count();
    console.log(count);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

to get the items returned from the query.

The rewind method lets us reset the cursor to its initial position in the set of returned documents.

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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
    };
    const cursor = testCollection.find(query);
    const firstResult = await cursor.toArray();
    console.log("First count: ", firstResult.length);
    await cursor.rewind();
    const secondResult = await cursor.toArray();
    console.log("Second count: ", secondResult.length);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We called rewind and get the result from the cursor before and after calling rewind .

We get the same result in each console log because we called rewind to reset the cursor.

Conclusion

We can listen to the data event and get the data from there.

Also, we can use the cursor to do various retrieval operations.

Categories
MongoDB Node.js Basics

Node.js Basics — MongoDB Query and Cursor

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.

Retrieve Data

We can retrieve data by using methods that comes with the MongoDB driver.

The simplest way to get items from a collection is to use the find 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 testCollection = await client.db("test").collection('test');
    const result = await testCollection.insertMany([
      {
        name: "Popeye",
        rating: 5,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
      qty: {
        $gte: 90,
        $lt: 110,
      },
    };
    const cursor = testCollection.find(query);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We make a query with the qty property to find the documents wirh the qty property greater than or equal to 90 and less than 110.

To find the first document in a collection that meets the condition given in a query, we can use the findOne method with the same argument.

The cursor has the forEach method to let us traverse the query results.

Aggregate

To make a query with a given criteria and then group them together, we can use the $match and $group properties together in a query.

For instance, 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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = [
      {
        $match: {
          qty: {
            $gte: 90,
            $lt: 110,
          },
        },
      },
      {
        $group: {
          _id: "$status",
          count: {
            $sum: 1,
          },
        },
      },
    ];
    const cursor = testCollection.aggregate(query);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

In the code above, we get all the entries with the qty property that’s greater than or equal to 90 and less than 110.

Then we group all the items by counting the entries with the $sum operator.

Now we should get the count of the number of items that are found with the query.

Access Data From a Cursor

We can access data from a cursor in different ways.

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,
        qty: 100
      },
      {
        name: "KFC",
        rating: 4,
        qty: 121
      },
    ]);
    console.log(result)
    const query = {
      name: "Popeye",
      qty: {
        $gte: 90,
        $lt: 110,
      },
    };
    const cursor = testCollection.find(query);
    await cursor.forEach(doc => console.log(doc));
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

query has the query that we want to make.

Then we call forEach on the cursor to loop through each entry.

Conclusion

With the MongoDB Node.js client, there are several ways we can use to retrieve the data we want.

Categories
MongoDB Node.js Basics

Node.js Basics — MongoDB Upserts and Queries

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.

Upserts

We can do upserts by setting the upsert option to true .

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.insertOne({
      name: "Popeye",
    });
    console.log(result)
    const query = { name: "Popeye" };
    const update = {
      $set: { name: "Popeye", address: "3 Nassau St" }
    };
    const options = { upsert: true };
    const updateResult = await testCollection.updateOne(query, update, options);
    console.log(updateResult)
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We set the upsert property in the options object to true so that we can insert the entry when it’s not available.

The $set property lets us set the properties we want to set.

Querying Data

We can query data in to search for in the collection and then do our updates to the results.

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.insertOne({
      name: "Popeye",
    });
    console.log(result)
    const query = { name: "Popeye" };
    const cursor = testCollection.find(query);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

to make a query to find the documents with the name set to 'Popeye' .

We call the find method to find all the entries with that value.

And then we call the forEach method to search for items.

We can also search for the items with a given quantity.

To do that, 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: { $eq: 5 }
    };
    const cursor = testCollection.find(query);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

We inserted 2 entries into our collection.

Then we make the query with the find method.

The $eq key means we want to search for the items that equals something.

Therefore, we search for any documents with the rating property equal to 5.

This is the same as:

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: 5
    };
    const cursor = testCollection.find(query);
    await cursor.forEach(console.dir);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

Conclusion

We can query items by using various operations supported by MongoDB.