nodejs
  1. nodejs-mongodb-select

Node.js MongoDB Select

In Node.js with MongoDB, the find() method is used to retrieve documents from a MongoDB collection. The find() method returns a cursor object, which can be used to iterate over the documents or transform the results. In this tutorial, we'll discuss how to use the find() method to select documents from a MongoDB collection in Node.js.

Syntax

The basic syntax for selecting documents from a MongoDB collection in Node.js with the find() method is as follows:

db.collection("collectionName").find(query, projection)
  • db.collection("collectionName"): specifies the name of the collection you want to select documents from.
  • find(query, projection): specifies the query and a projection (optional) to filter and retrieve documents from the collection.

Example

Let's say we have a collection of students called "students" with the following data:

{
  "_id": 1,
  "name": "John",
  "age": 20,
  "gender": "Male",
  "address": {
    "street": "34th Street",
    "city": "New York",
    "state": "NY",
    "zip": 10001
  }
},
{
  "_id": 2,
  "name": "Jane",
  "age": 22,
  "gender": "Female",
  "address": {
    "street": "Main Street",
    "city": "Los Angeles",
    "state": "CA",
    "zip": 90001
  }
}

To select all the documents from the "students" collection, we can use the following piece of code in Node.js:

const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/mydb', function(err, db) {
  if (err) throw err;
  const collection = db.collection('students');
  collection.find().toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

The above code will fetch all the documents from the "students" collection and print them in the console.

Output

When we run the above code, we will get the following output:

[ 
  { 
    _id: 1, 
    name: 'John', 
    age: 20, 
    gender: 'Male', 
    address: { 
      street: '34th Street', 
      city: 'New York', 
      state: 'NY', 
      zip: 10001 
    } 
  },
  { 
    _id: 2,
    name: 'Jane',
    age: 22,
    gender: 'Female',
    address: {
      street: 'Main Street',
      city: 'Los Angeles',
      state: 'CA',
      zip: 90001
    }
  }
]

Explanation

In the example above, we connected to the MongoDB database using the MongoClient module, and then selected the "students" collection using the db.collection() method. We then used the find() method to retrieve all the documents from the collection.

Finally, we used the toArray() method to convert the cursor object into an array and print the result.

Use

You can use the find() method to retrieve documents from a MongoDB collection based on certain conditions. This method supports a query and projection parameter to filter and project the results.

Important Points

  • The find() method does not return the result immediately. It returns a cursor object that can be used to traverse the documents or transform the results.
  • The toArray() method is used to convert the cursor object to an array.
  • The projection parameter accepts a document that specifies which fields to include or exclude in the result.

Summary

In this tutorial, we discussed how to use the find() method to select documents from a MongoDB collection in Node.js. We covered the syntax, example, output, explanation, use, and important points of find(). With this knowledge, you can now retrieve documents from a MongoDB collection using Node.js and find().

Published on: