Monk Driver for MongoDB – How to Select Documents?

Want create site? Find Free WordPress Themes and plugins.

There are a number of reasons why I’ve chosen the Monk driver over others. Refer the How to Create documents in MongoDB to know more about the benefits of using Monk Driver. Moving forward, in this blog I’ll show you how to select documents in MongoDB using Monk.

To connect to MongoDB using Monk, you have to first fire the following lines of code.

const db = require('monk')('localhost/mydb')
const users = db.get('dbusers')
db.close()

Now we are ready to get started. I’ve listed some ways with which you can fetch data from MongoDB with Monk Driver:
1. Find
Find() is one of the ways to fetch data from MongoDB, the same as, select() method in RDBMS.

Using find() Method:

To get All the values from particular collection

var collection=db.get(‘collectionName’);
collection.find({},{}, function (err, doc, next) {
//do your operations
})

To get conditional values from the collection:

collection.find({condition},{}, function (err, doc, next) {
//do your operations
})

To get the particular field value from collections:

collection.find({condition},{$fields:{“fieldname1”:1,”fieldname2”:1}}, function (err, doc, next) {
//do your operations
})

Note: We need to use fieldname:1 to show the fields and fieldname:0 to hide the field while fetching the data from the collection.

2) Aggregate()

Aggregate() is another method to fetch data from collection, which requires projecting the fieldname.
               Using Aggregate Methods:

var collection=db.get(‘collectionName’);
collection.aggregate([{aggregation}], function (err, doc, next) {
//do your operations
})

Following are the possible stages in aggregation framework −

i) $project − Used to select some specific fields from a collection.

  • Fetching the required field Without condition
collection.aggregate([{$project:{“fieldname”:1,”fieldname2”:1}}],function(err,doc,next){
//Do your operation })

ii) $match − As we don’t need all the results every time, therefore, to reduce the number of results, this operation is very useful. As the lesser the number of records fetched, faster will be the execution of query.

  • Fetching required field on the basis of condition :
collection.aggregate([{$match:{condition}},{$project:{“fieldname”:1,”fieldname2”:1}}],function(err,doc,next){//Do your operation })

iii) $group − returns the aggregation value based on group.

collection.aggregate([{$group:{_id:{”fieldname”}}],function(err,doc,next){//Do your operation })

Here it is to be noted that we need to use _id with fieldname on which we want to group the value.

Example :

collection.aggregate([{$group:{_id:{”firstname”}}],function(err,doc,next){//Do your operation })

will group the values on the basis of the firstname.

iv) $unwind − This method is used to unwind document using arrays. Sometimes we don’t need all the bunch of arrays in a subdocument. So to break the arrays into chunks we have this method.

Taking the example with given collection:

{_id:1, subdocument:[
{
“Key1” : “value1”,
“key2”:”value2”
},
{
“Key3” :”value3”
}
]
}

Query to be written to get all arrays of objects in the above subdocument:

collection.aggregate([{$unwind:”$subdocument”}],function(err,doc,next){
//our operation
})

It will show the result:

{_id:1,subdocument:{”key1”:”value1”,”key2”:”value2”}}
{_id:1,subdocument:{”key3”:”value3”}}

That’s all for today! We will continue updating the blog with more queries. Stay tuned.
Upcoming blog on ‘Updating in MongoDB using Monk.

Did you find apk for android? You can find new Free Android Games and apps.

Tags

Related Posts

Leave A Comment