Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Wednesday 29 July 2020

How do I manage MongoDB connections in a Node.js web application?

How do I manage MongoDB connections in a Node.js web application?

Question: How do I manage MongoDB connections in a Node.js web application?
You need to install mongoose and bluebird module.
Also you must have mongodb install in server, As you need mongodb URL and port on which mongodb running.

var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
//mongodb connection with error handing
mongoose.connect(config.MONGO_DB_URL + config.MONGO_DB);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.log("MongoDB connected Successfully.!");

Question: mongoose - find all documents with IDs listed in array?
var AjaxChatUser = mongoose.model('AjaxChatUser');
    '_id': { $in: [
}, function(err, docs){

Question: How to npm install to a specified directory?
Use --prefix option, to installed in specific directory.
npm install --prefix  -g

Question: How to declare multiple module.exports in Node.js?
You can put multiple function inside module.exports.

module.exports = {
    method: function() {},
    otherMethod: function() {},

Question: Can we write a JS code that work for both (node and the browser)?
Yes, We can write.
Suppose we have mymodule.js which have following code.
Example of Code
   exports.test1 = function(){
        return 'this is test1 function.'
   exports.test2 = function(){
        return 'this is test2 function.'

})(typeof exports === 'undefined'? this['mymodule']={}: exports);

In Node (Server side()
var share = require('./mymodule.js');

In Browser (Client side()
//Include the js with script tag

Question: How we can use global variable in Node?
We can use with global.varname

Question: How we can access static files with express.js in Node?
Use following to set the folder as static so that we can put public files here.
app.use(express.static('public')); //public folder

Thursday 10 October 2019

How to speed up your application with MongoDb?

How to speed up MongoDb?

1) Make Sure you application have Sufficient RAM
If your application don't have enough RAM, then please increase it.

2) Analyze Your Queries
Analyse the queries with explain like below:
db.user.find('{"likes": {$gt:10}"likes"}').explain("executionStats");
You can check the problem with the queries and fix the issue.
Also please fetch only those records which are using. Don't fetch un-necessary field and records from database.

3) Limit the Records
If you are using 10 records but getting 100 records. Means you are getting 99 record extra that is wrong. So please limit the record with limit command.
db.user.find('{"likes": {$gt:10}"likes"}').limit(10);

4) Add Appropriate Indexes
You can use the index wherever are required (most used in search query). You can add indexes on Single/Multiple field. In this way you can speed the application by speedup the queries.
db.user.createIndex({ country: 1 });

5) Sorting
Un-necessary sorting can also have problem, So sort the records wherever required. Avoid un-necessary sorting. Example of Sorting (-1 no sorting, 1 ascending , 0 descending )
Sort by Country and then City.
db.user.find().sort({ country: 1, city: 1 });

Sort by Country only (Even we have added city in our indexes).
db.user.find().sort({ country: 1, city: -1 });

Sort by city only (Even we have added country in our indexes).
db.user.find().sort({ country: -1, city: 1 });

6) Check Your MongoDB Log
As we know, logging the queries take some time to logged. It make take about 100Mili seconds for each log query.
So, You should log the queries carefully because un-necessary loging data may slow the system.
0 is the MongoDB's default log verbosity level, to include Informational messages.
1 to 5 increases the verbosity level to include Debug messages.

7) Understand the Query Profiling
You can get Profiling Level (current).

You can set Profiling Level.

Following the 3 Profiling Level.
  1. -1 for inherit the profiling from parent.
  2. 0 for no profiling.
  3. 1 for slow operations.
  4. 2 for all operations.

8) Multiple Database Connection
If you are running heavy site and few queries are taking taking more time due to which queries are completing in asynchronous.
Means query start time are q1, q2, q3 but completing time are q2,q1,q3.
To fix this problem.
For this, you can create multiple Database connection and handle like below:
  1. One to handle the majority of fast queries.
  2. One to handle slower document inserts and updates.
  3. One to handle complex report generation.

9) Set Maximum Execution time

MongoDB commands run as long as they need. A slowly-executing query can hold up others, and your web application may time out.
You can set the max execution time like below:
db.user.find('{"likes": {$gt:10}"likes"}').maxTimeMS(100);

10) Rebuild Your Indexes

You need re-index, if your collection fall in following:
  1. Collection size has increased significantly.
  2. Indexes are consuming a disproportionate amount of disk space.

Friday 27 September 2019

MongoDB Questions and Answers for beginners

MongoDB Questions and Answers for beginners

Question: What kind of NoSQL database used in MongoDB?
MongoDB is a document oriented database. It store data in form of BSON structure.

Question: What is a Namespace in MongoDB?
Namespace is concatenation of the database name and collection name.

Question: Does MongoDB support foreign key constraints?

Question: Does MongoDB support ACID transaction?

Question: Does MongoDB support Indexes?

Question: Does MongoDB support primary key & foreign key relationships in MongoDB?
No, By Default it does not support. But we can achieve this by using embedding one document inside another.

Question: How is MongoDB better than other SQL databases?
Namespace is concatenation of the database name and collection name.
  1. Highly flexible
  2. Scalable Document
  3. Fast
  4. Replication

Question: When we removed an document. Does it removed permanently?.
Yes, When we rmoved and document it delete from disk.

Question: When we removed an document. Does it removed permanently?.
Yes, When we rmoved and document it delete from disk.

Question: What happens if an index does not fit into RAM?.
In that case MongoDB reads data from disk which is relatively very much slower.

Question: What is a covered query in MongoDB?.
Covered query is the one in which, A) fields used in the query are part of an index
B) the fields returned in the results are in the same index.

Question: What is Aggregation in MongoDB??.
Aggregation is process of data-records and return computed results.

Question: What is Sharding in MongoDB??.
Sharding is a method for storing data across multiple machines (2 OR more machines).

Question: What is Replication in MongoDB?.
Replication is a process of synchronizing data across multiple servers.

Question: What are Primary and Secondary Replica sets?
Primary and master nodes are the nodes that can accept writes.
Secondary and slave nodes are read-only nodes that replicate from the primary.

Question: Why are MongoDB data files large in size?
MongoDB preallocates data files to reserve space and avoid file system fragmentation when you setup the server.

Question: What is a Storage Engine in MongoDB?
A storage engine is responsible for managing data like how data is stored on disk.

Question: What are two storage engines used by MongoDB?
1) MMAPv1
2) WiredTiger

Question: Why MongoDB is not preferred over a 32-bit system??
In 32-bit build system the total storage size for the server including data and indexes is 2GB.

Question: What is the role of a profiler in MongoDB??
The Profiler collects fine grained data about MongoDB write operations, cursors, database commands.

Question: How does MongoDB provide concurrency?
MongoDB uses reader-writer locks for the concurrency.

Question: What is hotfix for windows?
A hotfix is single/cumulative package to fix an issue.

Question: How to changes in storage path of MongoDB?
mongod --dbpath "d:\mongodb\data"

Question: How to get mongoDB Version ?
mongod --version

Wednesday 25 September 2019

MongoDB indexing and Embed document

MongoDB indexing and Embed document

Question: How to create indexing on single column?
1- Indexing in Ascending Order.

Question: How to create indexing on multiple column?
1- Indexing in Ascending Order.
-1- Indexing in Descendig Order.

Question: How to make unique key in MongoDB?
Now you can't add duplicate username in users collection.

Question: What is sparse in MongoDB indexing?
Sparse indexes only contain entries for documents that have the indexed field. The index skips over any document that is missing the indexed field. The index is sparse because it does not include all documents of a collection. .

Question: How to create indexing in background process?

Question: How to create indexing on column which can have duplicate values?

Question: How can we set index name while creating indexing?

Question: How to drop an index on single column?

Question: How to drop all indexes on document?

Question: How to update all index in collection?

Question: How to create indexing in Embed document?

Search Document From Embed document
db.users.find( { "" : "newYork" } )

Question: Compare "Multiple collections" vs "Embedded documents"?
  1. No much differences for insertion and updates document.
  2. Separate collections are better when you need to select individual documents and gives more control over querying.
  3. Embedded documents are better when embed document is less OR No More.
  4. Getting record from embedded documents is easy as compare Multiple collections.
  5. Embed document limit is 16MB
Question: What is Relationships in MongoDB?
Relationships denotes how various documents are logically related to each other.

Question: What are different ways to achieve the relationships in MongoDB?
Following are various two Approaches
1) Embedded Approaches 2) Referenced Approaches

Question: Given an example of Embedded?
    "_id": ObjectId("52ffc33cd85242f436099901"),
    "contact": "987654321",
    "dob": "01-01-1991",
    "name": "Tom Benzamin",
    "address": [
        "pincode": 123456,
        "city": "Los Angeles",
        "state": "California"
        "pincode": 456789,
        "city": "Chicago",
        "state": "Illinois"

Question: Given an example of Referenced?
    "_id": ObjectId("52ffc33cd85242f436099901"),
    "contact": "987654321",
    "dob": "01-01-1991",
    "name": "Tom Benzamin",
    "address": [

Sunday 22 September 2019

MongoDB Interview Question And Answers for 1 Year Experience

MongoDB Interview Question And Answers for 1 Year Experience

Question: How do I query mongodb with like ?
Following are few example of like as compare to SQL Query
/*select * from users where name like "%B%";*/
db.users.find({name: /B/});

/*select * from users where name like "B%"; */
db.users.find({name: /^B/});

/*select * from users where name like "%B"; */
db.users.find({name: /B$/});

Question: How to execute case in sensitive query?
/*select * from users where name like "%B%" OR like "%b%"; */
db.users.find({name: /b/i});

Question: How to list all collections in the mongoDB?
Following are three different methods.

show collections

show tables

Question: Pretty print in MongoDB shell?
Normally we use query as below:

For Pretty display, use like below:

Question: How to get the last N records?
/* -1 means descending order */

Question: How to export collection into CSV?
mongoexport --host localhost --db bank --collection users --csv --out e:/dump/mycsv.csv --fields name,age,gender

Question: How to import into collection from CSV?
mongoimport -d bank -c users --type csv --file e:/dump/mycsv.csv --headerline

Question: How can I rename a field for all documents in MongoDB?
db.students.updateMany( {}, { $rename: { "name": "full_name" } } )

Question: How to store date/time in mongodb
db.users.insert({date: ISODate("2014-02-10T10:50:42.389Z")})