If we want to read document from MongoDB collection using Node.js so we use find method. There are two methods one is FindOne() other is find().
Find One – Find Single Document from Collection
We have lot of documents in a collection, we will match the documents and find the single document out of the multiple documents that we match using findOne().
The findOne() method returns the first occurrence from the collection according to the ‘query’ object. if you will give nothing inside findOne like
findOne( { } , function (err, result) { }
this will return the first document from the collection.
//to import mongodb
var MongoClient = require("mongodb").MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//make client connect
MongoClient.connect(url, function (err, client) {
var db= client.db('mydb');
if (err) throw err;
//document to be find
var query = {name: 'ESI'};
// find document to 'customers' collection using findOne
db.collection("customers").findOne(query, function (err, result) {
if (err) throw err;
console.log(result);
client.close();
});
});
Write this code in your editor and save with js extension and run this at your terminal.
(i have saved above code with findone1.js)
Run Command
C:\Users\Your Name>node findone1.js
Result
{_id : 5a45ecedc0e02b0af0b6cda4,
name:'ESI',
address:'Highway 37'
}
Find – Find All Document from Collection
We have many documents in the collection, we will match the documents using findMany() and select all the documents that match and show then all at once.
//to import mongodb
var MongoClient = require("mongodb").MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//connect url
MongoClient.connect(url, function (err, client) {
var db= client.db('mydb');
if (err) throw err;
// find documents to 'customers' collection using find
db.collection("customers").find( { } ).toArray(function(err, result) {
if (err) throw err;
console.log(result);
client.close();
});
});
Write this code in your editor and save with js extension and run this at your terminal.
(i have saved above code with findmultiples1.js)
Run Command
C:\Users\Your Name>node findmultiples1.js
Result
This will show all the documents of ‘costumers’ collection.
Find all the documents using query object
//to import mongodb
var MongoClient = require("mongodb").MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//make client connect
MongoClient.connect(url, function (err, client) {
var db= client.db('mydb');
if (err) throw err;
//document to be Find
var query = {name: 'HEG'};
// find documents to 'customers' collection using find
db.collection("customers").find(query).toArray(function(err, result) {
if (err) throw err;
console.log(result);
// close the connection to client when you are done with it
client.close();
});
});
Write this code in your editor and save with js extension and run this at your terminal.
js (i have saved above code with findmultiples2.js)
Run Command
C:\Users\Your Name>node findmultiples2.js
Result
{ _id: 5a462752891b0202e0f8f0db,
name:'HEG',
address: 'Highway 12'},
{ _id: 5a4627e4149a6e2c1c2fcaad,
name:'HEG',
address: 'Highway 12'},
This will show all the document which will have named HEG.
Find using query with some operator
//to import mongodb
var MongoClient = require("mongodb").MongoClient;
//connect url
var url = "mongodb://localhost:27017/mydb";
//make client connect
MongoClient.connect(url, function (err, client) {
var db= client.db('mydb');
if (err) throw err;
//find using operator
db.collection("customers").find({$and:[{name:"HEG"},{address:"Highway 12"}]}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
client.close();
});
});
Write this code in your editor and save with js extension and run this at your terminal.
(i have saved above code with findmultiples3.js)
Run Command
C:\Users\Your Name>node findmultiples3.js
Result
This will give all the document who will satisfy both the conditions(name:’HEG’ and address:’highway 12′)
We can use many operator to extract different data from the collection.there are some operator we can use here,
⦁ $eq =Matches values that are equal to a specified value.
⦁ $gt =Matches values that are greater than a specified value.
⦁ $lt =Matches values that are less than a specified value.
⦁ $or =Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
⦁ $not =Inverts the effect of a query expression and returns documents that do not match the query expression.
Learn More-