Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (4k points)

I am using the mongodb driver and monk on nodejs. Examples such as this that I see on the Web have the following pattern:

var mongo = require('mongodb');
var monk = require('monk');

var db = monk('localhost:27017/userdb');
var collection = db.get('users');
collection.find({}, function(err, docs) {
    // do something with docs
});

Two questions:

  1. Why is the first line needed: var mongo = require('mongodb')? The variable mongo is never used. Wouldn't monk automatically require mongodb?
  2. I see at the driver level, the db has to be opened and closed. These methods don't seem to exist at the monk level. Does monk automatically open and close connections? How does this work?

As a matter of fact, I am wondering what advantage does monk provide over using the driver directly. I read the list of features in the monk docs, but don't really understand the benefits.

Thanks in advance for your help.

1 Answer

0 votes
by (8.7k points)
edited by

Understand with the help of below following points:

It is not necessary to have a MongoDB variable but it is required as a dependency in addition to the monk.

It is okay if you don’t make a call to db. close() as you wonder that a connection will be opened. This type of information is not being documented anywhere around.

The goal of the monk is to provide a more friendly API than provided by MongoDB's own driver.

If you are passionate to pursue a career in Web Development, you can take up Web Development Online Courses by Intellipaat!

Related questions

0 votes
0 answers
asked Mar 3, 2021 in Web Technology by adhiraj (4k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...