By default , if we perform any query on a mongodb collection it tends to return all the data of a field in the matching document.
So in order to avoid that we use projection. Projections limit the fields returned by the query.The projection document can specify the
inclusion as well as exclusion of the fields as shows below:
{ field-name-1 : value , field-name-2 : value .... so on}
here value can be either 1 or 0 where 1 signifies inclusion and 0 signifies exclusion
Step - 1 : Including Packages
We will start by
requiring the package.
We are using the following package in our application :
var mongo = require('mongodb');
Step - 2 : Establish Connection
Now let's establish a connection between the mongoDb Database and our node.js application.
var new_db = "mongodb://localhost:27017/demo_db"
Step - 3 : Projection
node read-all-occurance-mongodb-nodejs.js
Record Read successfully
[ { name: 'A',
section: 'arts',
roll: '2',
house: 'nilgiri',
_id: 599bf49b7e063437b867ed4c },
{ name: 'B',
section: 'commerce',
roll: '12',
house: 'shivalik',
_id: 599bf4c65ef24a38a44bdf17 },
{ name: 'C',
section: 'arts',
roll: '1',
house: 'nilgiri',
_id: 599bf4ec10eb8b3a288b83b8 },
{ name: 'E',
section: 'arts',
roll: '10',
house: 'shivalik',
_id: 599bf521beed503a009ee0b0 },
{ name: 'D',
section: 'commerce',
roll: '5',
house: 'nilgiri',
_id: 599bf5547157e933f803bfaf } ]
project()
is used along with find()
to perform projection. We are using the details collection
as dataset for our operation. we will be displaying the name , house field only.
//projection-mongodb-nodejs.js
var mongo = require('mongodb');
var new_db = "mongodb://localhost:27017/demo_db"
mongo.connect(new_db , function(error , db){
if (error){
throw error;
}
db.collection("details").find({ section : "arts"}).project({name:1 , house : 1}).toArray( (err , collection) => {
if(err) throw err;
console.log("Record Read successfully");
console.log(collection);
db.close();
});
});
>node projection-mongodb-nodejs.js
>node projection-mongodb-nodejs.js
Record Read successfully
[ { name: 'A', house: 'nilgiri', _id: 599bf49b7e063437b867ed4c },
{ name: 'C', house: 'nilgiri', _id: 599bf4ec10eb8b3a288b83b8 },
{ name: 'E', house: 'shivalik', _id: 599bf521beed503a009ee0b0 } ]
//projection-mongodb-nodejs.js
var mongo = require('mongodb');
var new_db = "mongodb://localhost:27017/demo_db"
mongo.connect(new_db , function(error , db){
if (error){
throw error;
}
db.collection("details").find({ section : "arts"}).project({_id:0 , name : 1}).toArray( (err , collection) => {
if(err) throw err;
console.log("Record Read successfully");
console.log(collection);
db.close();
});
});
>node projection-mongodb-nodejs.js
>node projection-mongodb-nodejs.js
Record Read successfully
[ { name: 'A' }, { name: 'C' }, { name: 'E' } ]
//projection-mongodb-nodejs.js
var mongo = require('mongodb');
var new_db = "mongodb://localhost:27017/demo_db"
mongo.connect(new_db , function(error , db){
if (error){
throw error;
}
db.collection("details").find({}).project({section : 0 , roll : 0}).toArray( (err , collection) => {
if(err) throw err;
console.log("Record Read successfully");
console.log(collection);
db.close();
});
});
>node projection-mongodb-nodejs.js
>node projection-mongodb-nodejs.js
Record Read successfully
[ { name: 'A', house: 'nilgiri', _id: 599bf49b7e063437b867ed4c },
{ name: 'B', house: 'shivalik', _id: 599bf4c65ef24a38a44bdf17 },
{ name: 'C', house: 'nilgiri', _id: 599bf4ec10eb8b3a288b83b8 },
{ name: 'E', house: 'shivalik', _id: 599bf521beed503a009ee0b0 },
{ name: 'D', house: 'nilgiri', _id: 599bf5547157e933f803bfaf } ]