Mongoose find() Function

The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).

Installation of mongoose module:

  1. You can visit the link to Install mongoose module Mongoose package installation link. You can install this package by using this command.
    npm install mongoose
  2. After installing mongoose module, you can check your mongoose version in command prompt using the command.
    npm version mongoose
  3. After that, you can just create a folder and add a file, for example index.js. To run this file you need to run the following command.
    node index.js

Filename: index.js

javascript
const mongoose = require('mongoose');

// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/w3wiki', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});

// User model
const User = mongoose.model('User', {
    name: { type: String },
    age: { type: Number }
});

// Only one parameter [query/condition]
// Find all documents that matches the
// condition name='Punit'
User.find({ name: 'Punit'}, function (err, docs) {
    if (err){
        console.log(err);
    }
    else{
        console.log("First function call : ", docs);
    }
});

// Only Two parameters [condition, query projection]
// Here age:0 means don't include age field in result 
User.find({ name: 'Punit'}, {age:0}, function (err, docs) {
    if (err){
        console.log(err);
    }
    else{
        console.log("Second function call : ", docs);
    }
});

// All three parameter [condition, query projection,
// general query options]
// Fetch first two records whose age >= 10 
// Second parameter is null i.e. no projections
// Third parameter is limit:2 i.e. fetch
// only first 2 records
User.find({ age: {$gte:10}}, null, {limit:2}, function (err, docs) {
    if (err){
        console.log(err);
    }
    else{
        console.log("Third function call : ", docs);
    }
});

Explanation

  • ‘mongoose.connect()’: This command used to established a connection to a mongoDb database.
  • ‘UserSchema’: It defines the structure of documents in the collection.
  • ‘User’: It represents the Mongoose model for the collection.
  • ‘User.find()’: This command allow to executes the find operation, specifying the query, projection and options.

Steps to run the program:

  1. The project structure will look like this:
  2. Make sure you have install mongoose module using following command:
    npm install mongoose
  3. Below is the sample data in the database before the find() function is executed, You can use any GUI tool or terminal to see the database, like we have used Robo3T GUI tool as shown below:
  4. Run index.js file using below command:
    node index.js

So this is how you can use the mongoose find() function in Node.js and MongoDB.