Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Monday, March 27, 2017

Efficient way of performing filtering, sorting and pagination with MongoDB C#

Problem: I run into issue where pagination was bringing almost all the rows from the collection and then doing pagination.  In other words it is doing in memory pagination which is inefficient.  Recommendation:  In order to do MongoDB pagination: avoid using LINQ Query.

Solution:
With following way you can easily and efficiently do filtering, sorting and pagination on mongoDB collection.  Below code snippet will bring back only records which are required.  Example:  If pageSize is 10 it will only bring back 10 records.  This code is tested on over 12 million records and it is working efficiently.  I am open for any comment/suggestion to make it more robust.

Note:  It uses predicate way of writing lambda expression.

C# code for filtering, sorting and pagination on MongoDB Collection

//Get MongoDB Collection
var collection = database.GetCollection("Member");


//Query the collection with criteria formed as lambda expression.
var filter = Builders
                 .Filter
                 .Where
                   (
                          a => a.City == "Ahmedabad" &&
                          a.DateOfBirth < DateTime.Now.AddYears(-18)
                    );


//Build sort definition based on your need
var sort = new SortDefinitionBuilder()
                       .Descending(x => x.DateOfJoin);


//Apply Pagination and return back list of records.
var resultSet = collection
                     .Find(filter)
                     .Sort(sort)
                     .Skip(pageSize * (currPage - 1))
                     .Limit(pageSize).ToList();


Monday, April 04, 2016

Install Mongodb and run as a service to start automatically for windows

This blog post will explain where to download mongodb, how to install and how to configure to start service automatically.  

Step 1: Download mongodb, I am using community edition.  Follow step by step installation process to install mongodb.

After installation a directory inside C:\Program Files is created.  
Example:  In my case:  C:\Program Files\MongoDB\Server\[version]

Please note: [version] can be current version you have downloaded.  Example: 2.4

Step 2:  Move the Mongodb directory to c:\ for easy access. i.e. C:\MongoDB\Server\[version]

Step 3:  Create directories inside C:\MongoDB\Server\[version]
mkdir C:\MongoDB\Server\[version]\data
mkdir C:\MongoDB\Server\[version]\logfiles

Step 4: Create mongod.conf file in C:\MongoDB\Server\[version], Add following content in it and save the file.
# Where data files will reside
dbpath=C:\MongoDB\Server\[version]\data

# Where the log file will be stored
logpath=C:\MongoDB\Server\[version]\data\logfiles\mongo-server.log

# how verbose the server will be logging
verbose=vvvvv

Step 5:
Open command line as administrator.
Type following commands to start mongodb as service, so that you don't have to manually start mongo server each time.

C:\MongoDB\Server\[version]\bin> mongod -f C:\MongoDB\Server\[version]\mongod.conf --install

C:\MongoDB\Server\[version]\bin>net start MongoDB

And you are good to go.  Your mongodb is up and running.

-----------------

For creating database, collection and document for mongodb you can download robomongo which is nice GUI for mongodb.


That's it.  It is that easy.

--------------------

In case you are interested in installing mongodb on ubuntu then following link will be helpful.
https://www.youtube.com/watch?v=CkDd6jClqEE
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04


Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape