Home » MongoDB

Method of using $lt in MongoDB

In this article, we are going to learn about the process and method of using $lt in MongoDB.
Submitted by Manu Jemini, on March 24, 2018

A record is a mongo document that is composed of field and value pairs. The documents are like JSON objects. Just like JSON in mongo we can put more documents inside a document.

$lt MongoDB means less than, to find a document in mongodb through an Express server we need to full fill certain requirements:

Read more in previous article: How to use $gt in MongoDB?

Now in the example below we have to search for the matching company name and quantity in this collection by using the function find;

    collection.find({company_name:"nissan", quantity: { $lt: 408} }).
    toArray(function(err,res){ })

Now, this function will search every document with the matching field of company name as “nisaan” and quantity less than 408.

Just like before we have to check for the error, if any. So to check if there is any error we use simple if-else statements.

The Object error will be passed by mongo itself and it will be false or null if there is no error, otherwise we will get an error object.

JS file:

// require mongodb
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
//create url
var url = "mongodb://localhost:27017/vehicle";
//connect with mongo client
MongoClient.connect(url, function(err,db){
	if(err)
	{
		console.log(err);
	}
	else
	{
		//console.log tthe connected url
		console.log('Connected to ',url);

		//get refernce of using collection
		var collection = db.collection('cars');

		// to update the documents

		collection.find({company_name:"nissan", quantity: { $lt: 408} }).toArray(function(err,res){
			if(err)
			{
				console.log(err);
			}
			else if(res.length)
			{
				console.log(res);
			}
			else
			{
				console.log('No car found');
			}
			db.close();
		})
	}
});

Output in console:

Method of using $lt in MongoDB

Output in shell:

{
        "_id" : ObjectId("5a6f608356bde820c878b31e"),
        "id" : "0001",
        "type" : "sedan",
        "company_name" : "nissan",
        "qty" : 200,
        "model_info" : {
                "model_name" : [
                        {
                                "id" : "1001",
                                "name" : "nissan sunny"
                        },
                        {
                                "id" : "1002",
                                "name" : "nissan sylphy"
                        },
                        {
                                "id" : "1003",
                                "name" : "nissan teana"
                        }
                ],
                "type" : [
                        {
                                "id" : "5001",
                                "name" : "Petrol"
                        },
                        {
                                "id" : "5002",
                                "name" : "Diesel"
                        }
                ]
        }
}


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.