Skip to main content


Showing posts from July, 2015

Indexing Limitations in MongoDB

This limitation regards to MongoDB version 2.6 and above.
1- As we know,  that  the indexes are stored in RAM, we should make sure that the index size does not exceed the RAM limit. If index size increases  than RAM will start deleting some indexes and hence causing performance loss.
2- Total size of an index entry must be less than 1024 bytes but remember it can include overhead depending on the BSON type.
3- If index entry of existing document exceed to index key limit, MongoDB will deny to create an index on a collection. In previous version mongoDB would not deny to create but will not index the document.
4- Re-indexing operation will give error if index entry field exceeds the index key limit.
5- Update to indexed field will give error if entry would exceed the index key limit.
6- A collection can't have more than 64 index.
7- The name length can't be more than 128 character.
8- A compound index can have maximum 31 fields indexed.

9- If you want to create 2dsphere index on a field…

What is the cursor in mongodb

As we know to fetch records we have to use find() method in mongodb. db.collection.find() method is the primary method for the read operation. This method queries a collection and returns a cursor to the queried document.

For the query performance, we need to iterate cursor, for example, suppose we have a collection with 20 million records and fire below the query 

this query will return data in more than lack, it may create an issue of the performance so we need to iterate of the cursor here.

In mongodb shell, if the returned cursor is not assigned to a variable like var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results. but if we are using it with c# then it will ask for iteration of cursor due to performance.

Note: You can use the DBQuery.shellBatchSize to change the number of iteration from the default value 20.
Cursor Behavior
Closure of inactive cursor: Server cl…

Analyze Query Performance

When we start programming, one thing come in our mind which is performance. There are many factors of performance but query optimization/analyzing and database structure is the more important.
In this article we will evaluate the performance of the query, Analyzing query is aspect of measurement of database and indexing effectiveness.
db.collection.explain("executionStats")  methods provide statistics about performance of query. This data output can be useful in measuring if and how a query uses an index.
Using $explain It's provides statistics about performance of query.
Evaluate the performance of query

{ "_id" : 1, "ISBN" : "22345654562349", type: " Paper", quantity: 500 } { "_id" : 2, " ISBN " : "3345678765678", type: " Paper ", quantity: 100 } { "_id" : 3, " ISBN " : "1232234543551", type: " Paper ", quantity: 200 } { "_id" : 4, " ISBN …

Covered Queries in MongoDB

According to mongoDB document overview of index,If your application queries a collection on a particular field or set of fields, then an index on the queried field or a compound index on the set of fields can prevent the query from scanning the whole collection to find and return the query results. For more information about indexes click here.
What is a Covered Query
Covered Query : All the fields in the query are part of an index and all fields returned in the query are in the same index.
 MongoDB matches the query conditions and returns the result using the same index without actually looking inside documents. Since indexes are present in RAM, fetching data from indexes is much faster as compared to fetching data by scanning documents.
Using Covered Queries Consider the following document in customers collection:

{    "_id": ObjectId("67667989hu9uh665"),    "contact": "987654321"    "gender": "M",    "name": "Di…

MongoDB 3.0: What's New

MongoDB 3.0 is the newest and most significant release of the world’s fastest growing database. The version radically expands the use cases for MongoDB, allowing you to use it for the vast majority of all new applications. By improving performance by 7x - 10x, reducing storage needs by up to 80%, and lowering operations overhead by up to 95%, MongoDB 3.0 also dramatically increases your ability to deliver apps to market faster and more efficiently than ever before.

Download the white paper to learn about the latest features in 3.0:
    Pluggable storage engines, including WiredTiger    Higher performance & efficiency    Simplified operations through Ops Manager    End-to-end auditing    Enhanced Query Language and Tools
Companies ranging from startups to Fortune 500s choose MongoDB to build, improve, and innovate:

For more info click here

MongoDB - Relationships

It's represent how several types of documents are logically related to each other. Relationship can be pattern via embedded and referenced approaches like  1:1, 1: N, N: 1 or N: N.
We will consider the example of storing addresses for users. Suppose one user can have multiple addresses making this a 1:N relationship. Following is the sample document structure of user document:
{    "_id":ObjectId("52mmm33cd85242f436000001"),    "name": "Dilip",    "contact": "987654321",    "dob": "01-01-1991" }

given below  is the sample document structure of address document:
{    "_id":ObjectId("52ffc4a5d85242602e000054"),    "building": "22 A",    "pincode": 123456,    "city": "Delhi",    "state": "Delhi" }

Modeling Embedded Relationships In this approach, we will set the address document inside the user document.

{    "_id"…

MongoDB - Sharding

Sharding is responsible to storing data records across multiple machines. As per demand to manage large amount of data, a single machine is not sufficient to store data nor provide an acceptable medium to read and write data. To resolve this problem MongoDB provide a process called Sharding. Sharding solve the problem with horizontal scaling. Through sahrding, you may add more machines to support data growth and demands of read and write operations. Why Sharding? In replication all writes go to master nodeLatency sensitive queries still go to masterSingle replica set has limitation of 12 nodesMemory can't be large enough when active dataset is bigLocal Disk is not big enoughVertical scaling is too expensive Sharding in MongoDBBelow given diagram shows the sharding in MongoDB using sharded cluster.

Shards: It is used to store data and provide high availability and data consistency , shard is a separate replica in production environment.
Config Servers: Config servers store the c…

MongoDB- Replication

Replication is the process of synchronizing data across multiple servers.Provides redundancy and increases data availability with multiple copies of data of different database servers.It is protects a database from the loss of a single server and recover from hardware failure and service interruptions.Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup. Why we need to use Replication: 1-It is a process to keep your data safe , 2-high availability of data, 3-disaster recovery , 3-no down time for maintenance , 4-read scaling and it is  a transparent to the application. How replication works in MongoDBA replica set is a group of MongoDB instances that host the same data set. There is two node first is primary node that receives all write operations and secondary, apply operations from the primary so that they have the same data set. Replica set can have onl…

Indexing in MongoDB

As we know index is technique to arrange data in database which support the efficient resolution of queries. If we run a query to select data, MongoDB do scan every document of collection. Scanning of all document may affect performance of MongoDB when it process the large volume of data. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in index. The ensureIndex() MethodIn MongoDB to create an index we need to use ensureIndex() method. Syntax: Basic syntax of ensureIndex() method is given below:


In MongoDB if we want to create index in ascending order the we will use 1 or descending order to use -1.  Example

In ensureIndex() method we can pass multiple fields, to create index on multiple fields.

ensureIndex() method also accepts list of options (which are optional),…