DEV Community

Cover image for Mongoose find() Query : operators gt , lt ,gte, lte, in , all, nin # Challenge Your Skills
swapnanilWebDeveloper
swapnanilWebDeveloper

Posted on • Edited on

Mongoose find() Query : operators gt , lt ,gte, lte, in , all, nin # Challenge Your Skills

1) Query in mongoose is very useful and you can perform multiple quires by find() method.
2) Here you can use many operators like -> $gt, $lt, $gte, $lte.
3) For arrays in mongoose you can use some operators like => $all, $in, $nin

Let's go through this code to understand the multiple combinations that you can make by find() method.

To know more about mongoose and enhance your knowledge you can go to my github account
click here to get the github aaccount


const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
  await mongoose.connect('mongodb://127.0.0.1:27017/StudentData');

  const studentSchema = new mongoose.Schema({
    name: {
        first : String,
        last : String,
    },
    email : String,
    address : {
        state : String,
        district : String,
        city : String,
    },
    science : {
        physics : Number,
        chemistry : Number,
        mathematics : Number,
    },
    hobbies : [String],
  });

  const Student = mongoose.model('Student', studentSchema);

   await Student.insertMany([
    { 
        name : {
            first : "Mayuk",
            last : "Mukherjee",
        },
        email : "Mayuk@gmail.com",
        address : {
            state : "California",
            district : "Atlanta",
            city : "Georgia",
        },
        science : {
            physics : 99,
            chemistry : 88,
            mathematics : 97,
        },
        hobbies : ["sports", "singing", "dancing", "painting"],
    },
    { 
        name : {
            first : "Suryendu",
            last : "Sarkar",
        },
        email : "Suryendu@gmail.com",
        address : {
            state : "Texas",
            district : "Atlanta",
            city : "Denver",
        },
        science : {
            physics : 74,
            chemistry : 82,
            mathematics : 86,
        },
        hobbies : ["reading", "singing", "blogging", "sculpting"],
    },
    { 
        name : {
            first : "Aninda",
            last : "Mukherjee",
        },
        email : "Aninda@gmail.com",
        address : {
            state : "Massachuttes",
            district : "Atlanta",
            city : "Georgia",
        },
        science : {
            physics : 82,
            chemistry : 94,
            mathematics : 75,
        },
        hobbies : ["painting", "travelling", "sports", "judo"],
    },
    { 
        name : {
            first : "Sanlap",
            last : "Gadai",
        },
        email : "Sanlap@gmail.com",
        address : {
            state : "Alabama",
            district : "Oklohama",
            city : "Atlanta",
        },
        science : {
            physics : 85,
            chemistry : 66,
            mathematics : 79,
        },
        hobbies : ["coocking", "drawing", "sports", "gymming"],
    },
   ]);

   const allStudent = await Student.find({});
  // console.log(allStudent);

  // name.first = "Mayuk", name.last = "Mukherjee"
   const result1 = await Student.find({ 'name.first' : 'Mayuk', 'name.last' : "Mukherjee",  'science.physics' : { $gte: 98 }, 
              'science.chemistry' : { $lte : 90, $gte : 85} }, 'name address.state address.district address.city hobbies');

   console.log(result1); 

   const result2 = await Student.find({ 'address.state' : /Texas/i, 'address.district' : /Atlanta/i, 'science.mathematics' : {$gt : 80, $lte : 90} }, 
                      'name.first name.last science.physics science.chemistry science.mathematics hobbies' );
   console.log(result2);

   const result3 = await Student.find({  'science.physics' : { $gte: 80, $lte : 85 }, 'science.chemistry' : { $gte : 90, $lte : 95},
                   'name.last' : "Mukherjee" }, 'name.first name.last address science.physics science.chemistry science.mathematics');
   console.log(result3);

   //likes: { $in: ['vaporizing', 'talking'] }
    const result4 = await Student.find({ 'name.first': /Sanlap/i, 'address.city' : /Atlanta/i , 'science.mathematics' : {$gt : 75, $lte : 85} }, 
                    'name address.state address.district address.city science hobbies');

   console.log(result4);

   const result5 = await Student.find({ hobbies : { $in : ['painting','sculpting'] }}, 'name.first name.last address science.chemistry hobbies');
   console.log(result5);

   const result6 = await Student.find({ hobbies : { $all : ["painting", "travelling", "sports", "judo"] }}, 'name address.state address.district address.city science hobbies');
   console.log(result6);

   const result7 = await Student.find({ hobbies : { $nin : ["gymming", "judo", "sculpting"] }}, 'name.first name.last address science.physics science.chemistry science.mathematics hobbies');
   console.log(result7);

}
Enter fullscreen mode Exit fullscreen mode

Check this output here

PS C:\Users\USER\Downloads\mongoExpress\Queries_find> node src/find_1.js
[
  {
    name: { first: 'Mayuk', last: 'Mukherjee' },
    address: { state: 'California', district: 'Atlanta', city: 'Georgia' },
    _id: new ObjectId('65c513f52ab43c3d210b1659'),
    hobbies: [ 'sports', 'singing', 'dancing', 'painting' ]
  }
]
[
  {
    name: { first: 'Suryendu', last: 'Sarkar' },
    science: { physics: 74, chemistry: 82, mathematics: 86 },
    _id: new ObjectId('65c513f52ab43c3d210b165a'),
    hobbies: [ 'reading', 'singing', 'blogging', 'sculpting' ]
  }
]
[
  {
    name: { first: 'Aninda', last: 'Mukherjee' },
    address: { state: 'Massachuttes', district: 'Atlanta', city: 'Georgia' },
    science: { physics: 82, chemistry: 94, mathematics: 75 },
    _id: new ObjectId('65c513f52ab43c3d210b165b')
  }
]
[
  {
    name: { first: 'Sanlap', last: 'Gadai' },
    address: { state: 'Alabama', district: 'Oklohama', city: 'Atlanta' },
    science: { physics: 85, chemistry: 66, mathematics: 79 },
    _id: new ObjectId('65c513f52ab43c3d210b165c'),
    hobbies: [ 'coocking', 'drawing', 'sports', 'gymming' ]
  }
]
[
  {
    name: { first: 'Mayuk', last: 'Mukherjee' },
    address: { state: 'California', district: 'Atlanta', city: 'Georgia' },
    science: { chemistry: 88 },
    _id: new ObjectId('65c513f52ab43c3d210b1659'),
    hobbies: [ 'sports', 'singing', 'dancing', 'painting' ]
  },
  {
    name: { first: 'Suryendu', last: 'Sarkar' },
    address: { state: 'Texas', district: 'Atlanta', city: 'Denver' },
    science: { chemistry: 82 },
    _id: new ObjectId('65c513f52ab43c3d210b165a'),
    hobbies: [ 'reading', 'singing', 'blogging', 'sculpting' ]
  },
  {
    name: { first: 'Aninda', last: 'Mukherjee' },
    address: { state: 'Massachuttes', district: 'Atlanta', city: 'Georgia' },
    science: { chemistry: 94 },
    _id: new ObjectId('65c513f52ab43c3d210b165b'),
    hobbies: [ 'painting', 'travelling', 'sports', 'judo' ]
  }
]
[
  {
    name: { first: 'Aninda', last: 'Mukherjee' },
    address: { state: 'Massachuttes', district: 'Atlanta', city: 'Georgia' },
    science: { physics: 82, chemistry: 94, mathematics: 75 },
    _id: new ObjectId('65c513f52ab43c3d210b165b'),
    hobbies: [ 'painting', 'travelling', 'sports', 'judo' ]
  }
]
[
  {
    name: { first: 'Mayuk', last: 'Mukherjee' },
    address: { state: 'California', district: 'Atlanta', city: 'Georgia' },
    science: { physics: 99, chemistry: 88, mathematics: 97 },
    _id: new ObjectId('65c513f52ab43c3d210b1659'),
    hobbies: [ 'sports', 'singing', 'dancing', 'painting' ]
  }
]

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
swapnanilwebdeveloper profile image
swapnanilWebDeveloper

This is find() query , a very useful concept in mongoose if you find it difficult to understand or any queries regarding it let me know or comment on my post