Thursday, April 30, 2020

Node.js MongoDB Queries

Node.js MongoDB


nodejs mongodb query


Queries


We will learn how to to find the data from the database in this tutorial. To find documents in a collection we can filter the results using the query object. This query object is passed as the first argument of the find() method. We can limit the search with the help of this query object. 

If you have not learned about how to create a collection in MongoDB with Node.js and place the documents in that collection with node JS, then read the previous tutorial

This tutorial includes

  • The find() method
  • Query Object
  • AND query
  • OR query
  • Conditional queries
  • Regular Expression Query
  • Special Query Options
  • Querying inside Objects and Arrays
  • Query Options
  • Paging
  • Sorting

The find() method

The syntax of the find() method on Collection is,

collection.find(query[[[, fields], options], callback]); 
  • The “query” is an object representing query and specifying the conditions the documents need to be passed.
  • The “fields” represent the fields that should be contained by the response (by default all fields are included)
  • The “options” provide extra logic such as sorting options, paging, etc.
  • The “raw” driver is used to supply documents as BSON binary Buffer objects (by default value is false)
  • The “callback” has two arguments. The first argument an error object (if case some error occurs). The second argument is a cursor object.

To select entire data from a collection we can use {} as a query object.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({}) .toArray(function(error, result) {
    //Query object is left {}
    if (error) throw error;
    console.log("===========Selected Documents===========");
    console.log(result);
    database.close();
  });
});

Output:
===========Selected Documents===========
[
  {
    _id: 5ea81ce29e5f1044ec12710b,
    name: 'Jhony',
    dept: 'HR',
    salary: '10000'
  },
  {
    _id: 5ea8223ffb74b00a3889b2f5,
    name: 'Shiva',
    dept: 'Sales',
    salary: '15000'
  },
  {
    _id: 5ea8223ffb74b00a3889b2f6,
    name: 'Alex',
    dept: 'Sales',
    salary: '12500'
  },
  {
    _id: 5ea8223ffb74b00a3889b2f7,
    name: 'Viraj',
    dept: 'Marketing',
    salary: '20000'
  }
]

The Query Object


We can make selection limited by applying an appropriate query object. For example, if we want to find a document with a given name, then we can pass {name:'any_name'} as a query object.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({name:'Shiva'}) .toArray(function(error, result) {
    //Query object is set {name:'Shiva'}
    if (error) throw error;
    console.log("===========Selected Document===========");
    console.log(result);
    database.close();
  });
});

Output:
===========Selected Document===========
[
  {
    _id: 5ea8223ffb74b00a3889b2f5,
    name: 'Shiva',
    dept: 'Sales',
    salary: '15000'
  }
]

AND Query

If more than one field is specified than it is a AND query.

For example,

{ 
key1: "val1", 
key2: "val2" 
}

This query will return all records where key1 is “val1” and key2 is “val2”.

OR Query

OR queries can be performed with the $or operator. Query operator consumes an array containing multiple query objects. At least one of these query objects must match a document to get any results.

{
  $or:[
    {name:"Shiva"},
    {dept:"HR"}
  ]
}

For example,

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({
  $or:[    //OR Query
    {name:"Shiva"},
    {dept:"HR"}
  ]
}
) .toArray(function(error, result) {
    if (error) throw error;
    console.log("===========Selected Documents===========");
    console.log(result);
    database.close();
  });
});

Output:
===========Selected Document===========
[
  {
    _id: 5ea81ce29e5f1044ec12710b,
    name: 'Jhony',
    dept: 'HR',
    salary: '10000'
  },
  {
    _id: 5ea8223ffb74b00a3889b2f5,
    name: 'Shiva',
    dept: 'Sales',
    salary: '15000'
  }
]

Conditional Query

The Query object does not support operators like <, <=, >, >= and !=. However, this can be achieved with their aliases $lt, $lte, $gt, $gte and $ne. To match the values with conditional, the value needs to be wrapped in a separate object. 

{"salary":{$gte:10000}}
OR
{"salary":{$gte:10000,$lte:15000}}

We can also mix them to create ranges.

For example,

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({"salary":{$gte:"12500",$lte:"15000"}}) .toArray(function(error, result) {
    if (error) throw error;
    console.log("===========Selected Documents===========");
    console.log(result);
    database.close();
  });
});

Output:
===========Selected Documents===========
[
  {
    _id: 5ea8223ffb74b00a3889b2f5,
    name: 'Shiva',
    dept: 'Sales',
    salary: '15000'
  },
  {
    _id: 5ea8223ffb74b00a3889b2f6,
    name: 'Alex',
    dept: 'Sales',
    salary: '12500'
  }
]

Regular Expression query

We can also use a regular expression to filter the results. For example, if we want to find the documents with the name field starting with "S".

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({name: /^S/}) .toArray(function(error, result) {
    //Query object is set {name: /^S/} //RegExp
    if (error) throw error;
    console.log("===========Selected Document===========");
    console.log(result);
    database.close();
  });
});

Output:
===========Selected Document===========
[
  {
    _id: 5ea8223ffb74b00a3889b2f5,
    name: 'Shiva',
    dept: 'Sales',
    salary: '15000'
  }
]

Special query operators 


There are few other operators like OR and conditionals to perform some special operations.
  • The ‘$in’ defines an array of possible matches, {" field_name ":{$in:[1,2,3]}}
  • The ‘$nin’ defines an array of not required matches
  • The ‘$all’ is used to check that array value must match to the condition {" field_name ":{$all:[1,2,3]}}
  • The ‘$exists’ is used to check if a given field exists or not {"field_name":{$exists:true}}
  • The ‘$mod’ is used to check for a modulo {" field_name ":{$mod:{7,3}} is equivalent to " field_name " % 7 == 3
  • The ‘$size’ is used to check the size of an array value {" field_name ": {$size:5}} matches arrays name with 5 elements

Querying inside the Objects and Arrays


If the documents are nested then they can be easily retrieved by the (.) operator. For example, we have an object such as,

{
  "_id": id,
  "name":{
    "firstname":"Shiva",
    "lastname": "Kumar"
  },
  "address":[
    {
      "city":"Delhi",
      "street": "1/714",
      "state": "Delhi"
    }
  ]
}

We can write the query objects like,

collection.find({"name.firstname":"Shiva"})
OR
collection.find({"address.city":"delhi"})

Query options


Query options specify the appearance and the behavior of the query execution.

For example,

var query_options = {
    "limit": 20,
    "skip": 10,
    "sort": "title"
}

collection.find({}, query_options).toArray(...); 

Paging


Paging is applied by 'limit' and 'skip' options.

{
  "limit": 20,
  "skip": 10
}

can be applied to find 10 elements starting from 20.

Sorting


Sorting can be applied using the 'sort' parameter.

{
  "sort": [['field_name1','asc'], ['field_name2','desc']]
}

Tuesday, April 28, 2020

Node.js MongoDB Create Insert find document in a collection


Node.js MongoDB


Create a Collection, Insert and Find a document in a Collection

In this tutorial on Node.js and MongoDB, we will learn
  • Creating a Collection
  • What are Collection and Document?
  • Inserting a Document in a Collection
  • Inserting Multiple Documents in a Collection
  • Select(Find) the Documents from Collection

Creating a Collection


First, download the MongoDB Driver, Connect to MongoDB and create a Database, then create a collection in the database.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
 
MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.createCollection("customers", function(error, result) {
    if (error) throw error;
    console.log("Collection created!");
    database.close();
  });
});
/*
Output:
Collection created!
*/


What are Collection and Document?




MongoDB is a NoSQL database, where data is stored as collections of documents. The conventional relational SQL databases store records as rows of data in the tables, and tables related to each other. Document-oriented NoSQL databases like MongoDB have documents that are similar to a record in a collection. A collection is similar to the table. However, the collections are more independent than tables and can contain embedded documents.

MongoDB Collection
Collection


Inserting a document into Collection


To insert single data into a Collection, the insertOne() method is used.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  var doc = { name: "Jhony", dept: "HR", salary: "10000" };
  dbo.collection("employee").insertOne(doc, function(error, result) {
    if (error) throw error;
    console.log("One document is inserted successfully");
    database.close();
  });
});
/*
Output:
One document is inserted successfully
*/

Inserting Multiple documents to the Collection


We can add multiple documents to a Collection together with the insertMany() method. The first parameter of the insertMany() method is an array of document objects.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  //Inserting multiple Documents
  var docs = [
            { name: "Shiva", dept: "Sales", salary: "15000" },
            { name: "Alex", dept: "Sales", salary: "12500" },
            { name: "Viraj", dept: "Marketing", salary: "20000" }
              ];
  dbo.collection("employee").insertMany(docs, function(error, result) {
    if (error) throw error;
    console.log("Multiple Documents inserted successfully");
    database.close();
  });
});
/*
Output:
Multiple Documents inserted successfully
*/

Select(Find) the Documents from Collection


We can use the find() and findOne() method to get the data from the database. This is similar to the SELECT SQL query in conventional relational databases.

findOne() method

This method returns the first document of the collection. The first parameter in this method is the query object.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").findOne({}, function(error, result) {
    if (error) throw error;
    console.log(result.name+'\t'+result.dept+'\t'+result.salary);
    database.close();
  });
});
/*
Output:
Jhony   HR      10000
*/

find() method


To select entire data from a collection in MongoDB, the find() method can be used. It works like a SELECT statement and returns all occurrences in the selection. The find() method with no parameters is similar to SELECT * in SQL. A first parameter is a query object in this method. For example, an empty query object is used in the following code, to select all documents in the collection employee.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({}).toArray(function(error, result) {
    if (error) throw error;
    console.log("===========All Documents==============");
    for(doc of result)
    console.log(doc.name+'\t'+doc.dept+'\t'+doc.salary);
    database.close();
  });
});
/*
Output:
===========All Documents==============
Jhony   HR      10000
Shiva   Sales   15000
Alex    Sales   12500
Viraj   Marketing       20000
*/


The Second parameter of the method find() is used to get selected (limited) projection of the results.
For example,

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({}, { projection: { _id: 1, name: 1, dept:1} }) .toArray(function(error, result) {
    if (error) throw error;
    console.log("===========Selected Documents===========");
    console.log(result);
    database.close();
  });
});
//OUTPUT:
===========Selected Documents===========
[
  { _id: 5ea81ce29e5f1044ec12710b, name: 'Jhony', dept: 'HR' },
  { _id: 5ea8223ffb74b00a3889b2f5, name: 'Shiva', dept: 'Sales' },
  { _id: 5ea8223ffb74b00a3889b2f6, name: 'Alex', dept: 'Sales' },
  { _id: 5ea8223ffb74b00a3889b2f7, name: 'Viraj', dept: 'Marketing' }
]

We can not specify both 0 and 1 values in the same object (except if one of the fields is the _id field). If we provide a field with the value 0, all other fields get the value 1, and vice versa.

const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(error, database) {
  if (error) throw error;
  var dbo = database.db("testdb");
  dbo.collection("employee").find({}, { projection: { _id: 0, name: 1, dept:1} }) .toArray(function(error, result) {
    if (error) throw error;
    console.log("===========Selected Documents===========");
    console.log(result);
    database.close();
  });
});
//Output
===========Selected Documents===========
[
  { name: 'Jhony', dept: 'HR' },
  { name: 'Shiva', dept: 'Sales' },
  { name: 'Alex', dept: 'Sales' },
  { name: 'Viraj', dept: 'Marketing' }
]

Node.js MongoDB installation and creating Database Connection

Node.js MongoDB 


node mongodb database


Installation and creating Database Connection


MongoDB


MongoDB is a very popular and leading NoSQL database application. It is cross-platform and has a document-oriented design which is used to store the data in JSON-like documents with Schema.

First, we need to have access to the database to work with MongoDB. MongoDB is freely available and it can be downloaded from, https://www.mongodb.com.

Choose the correct version of MongoDB according to your Operating system and architecture and install it to your machine.

Install the MongoDB Driver


To install the MongoDB driver, just open the Node.js command prompt and execute the install command.

C:\Users\Your Name>npm install mongodb  

This driver provides both callback-based and Promise-based interaction with MongoDB. This driver provides a high-level API on top of the mongodb-core that is available for the developers to use.

Once the driver is downloaded and installed, Node.js can use this module to access and manipulate the MongoDB databases.

var mongo = require('mongodb');    


Creating a Database


To create a database in MongoDB, First, we need to create the MongoClient object, and then call the connect() method on this object. We need to specify a connection URL with the IP address (localhost in case if the database is available on the local machine), port number, and the database name.

    //including the MongoDB Driver
    const MongoClient = require('mongodb').MongoClient;
    const url = "mongodb://localhost:27017/testdb";

    MongoClient.connect(url, function(error, database) {
      if (error) throw error;
      console.log("Successfully, created Database!");
      database.close();
    });
/*
Output:
Successfully, created Database!
*/


However, in MongoDB, a database is actually never created until it has some content.

The database is only created when a collection (table) is created in it with at least one document (record).