Performing CRUD Operation with MongoDB using RSS Feed as an use case.

Rajnilari2015
Posted by in NoSql category on for Beginner level | Points: 250 | Views : 1515 red flag

This article will demonstrate CRUD Operation with MongoDB using RSS Feed as an use case.


 Download source code for Performing CRUD Operation with MongoDB using RSS Feed as an use case.

Recommendation
Read Installaton guide of MongoDB on Windows 64 bit machine before this article.

Introduction

MongoDB is a Document Store Database that stores semi-structured or unstructured records/documents in JSON format.

Earlier we have published an article as Installaton guide of MongoDB on Windows 64 bit machine. In this article, we will extend that further and perform a simple CRUD operation on that by taking RSS feed as an example.

Why we are using RSS Feed as an usecase ?

It's an expected question. But before going to answer, we would like to recommend to refer What is NoSQL? in order to get a better understanding. Now let us look into what RSS Feed is... Quoting Wikipedia

RSS (Rich Site Summary; originally RDF Site Summary; often called Really Simple Syndication) uses a family of standard web feed formats to publish frequently updated information: blog entries, news headlines, audio, video. An RSS document (called "feed", "web feed" or "channel") includes full or summarized text, and metadata, like publishing date and author's name.

We can figure out from the statement that, a RSS is purely a semi-structured/un-structured document data and it cannot be fit into a proper RDBMS or rather to say a RDBMS would not be a right choice for such data. Henceforth, is the inclination for NoSQL Document DB for which we will use MongoDB.

Environment Setup

We hope that MongoDB has been setup properly into the system. Fire up Visual Studio, and let us create a Console Application

Let us add the MongoDB.Driver, which is the official .Net driver for MongoDB.

If every thing goes smooth, we will receive

Using the code

Let us first start by creating a model as under

using System;

namespace ConsoleApplication1
{
    public class RSS
    {
        public Guid Id { get; set; }
        public string QuestionID { get; set; }
        public string QuestionTitle { get; set; }
        public string QuestionDescription { get; set; }
        public DateTime PublishDate { get; set; }
    }
}

This is a simple RSS class with some properties.

Let us write the DBOperation class as under

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class MongoDBOperation
    {      

        /// <summary>
        /// Insert RSS Feed
        /// </summary>
        /// <param name="rssFeed"></param>
        /// <returns></returns>
        public Guid InsertRssFeed(RSS rssFeed)
        {            
             MongoClient client = new MongoClient(); //Initializes a new instance of the MongoClient class.
             var database = client.GetDatabase("CRUDDemo");//Provide a database name(Mongo server will check a database with this name while inserting.)
             var collection = database.GetCollection<BsonDocument>("RSSFeed");//Provide a table Name(Mongo will create a table with this name)

             var doc = rssFeed.ToBsonDocument();  //Convert RSS to BSonDocument

            collection.InsertOne(doc); //Inserts a single document

            return BsonSerializer.Deserialize<RSS>(doc).Id; //Deserialize the BsonDocument and return the ID's
        }

        /// <summary>
        /// Update RSS Feed
        /// </summary>
        /// <param name="rssFeed"></param>
        /// <returns></returns>
        public Tuple<RSS,RSS> UpdateRssFeed(RSS rssFeed)
        { 

            MongoClient client = new MongoClient();//Initializes a new instance of the MongoClient class.
            var database = client.GetDatabase("CRUDDemo");//Provide a database name(Mongo server will check a database with this name while inserting.)
            var collection = database.GetCollection<BsonDocument>("RSSFeed");//Provide a table Name(Mongo will create a table with this name)

            var filter = Builders<BsonDocument>.Filter.Eq("QuestionID", rssFeed.QuestionID); //Create the Filter criteria by QuestionID
            var update = Builders<BsonDocument>.Update //Build the update definition for the fields to be updated
               .Set("QuestionTitle", "This is an updated question title")
               .Set("QuestionDescription", "This is an updated question");

            var result = collection.UpdateOne(filter, update);//Updates a single document from RSSFeed collection

            var doc = collection.Find(new BsonDocument()).ToList()[0]; //Find/Get the updated record

            return Tuple.Create(rssFeed, BsonSerializer.Deserialize<RSS>(doc)); //Return the old and modified record for further tally
           
        }

        /// <summary>
        /// Delete RSS Feed
        /// </summary>
        /// <param name="rssFeeds"></param>
        /// <returns></returns>
        public List<RSS> DeleteRssFeed(List<RSS> rssFeeds)
        {
            var rssCollection = new List<RSS>();

            MongoClient client = new MongoClient();//Initializes a new instance of the MongoClient class.
            var database = client.GetDatabase("CRUDDemo");//Provide a database name(Mongo server will check a database with this name while inserting.)
            var collection = database.GetCollection<BsonDocument>("RSSFeed");//Provide a table Name(Mongo will create a table with this name)

            var filter = Builders<BsonDocument>.Filter.Eq("QuestionID", rssFeeds[0].QuestionID); //Create the Filter criteria by QuestionID
            var result = collection.DeleteOne(filter); //Deletes a single document from RSSFeed collection
            collection.Find(new BsonDocument()).ToList().ForEach(i => rssCollection.Add(BsonSerializer.Deserialize<RSS>(i)));//Deserialize the BsonDocument and add the  to RSS collection

            return rssCollection;
        }

        /// <summary>
        ///  Read RSS Feed
        /// </summary>
        /// <returns></returns>
        public List<RSS> ReadRssFeed()
        {
            var rssCollection = new List<RSS>();

            new MongoClient()//Initializes a new instance of the MongoClient class.
             .GetDatabase("CRUDDemo")//Provide a database name(Mongo server will check a database with this name while inserting.)
             .GetCollection<BsonDocument>("RSSFeed")//Provide a table Name(Mongo will create a table with this name)
             .Find(new BsonDocument()) //Get All Records
             .ToList()
             .ForEach(i => rssCollection.Add(BsonSerializer.Deserialize<RSS>(i))); //Deserialize the BsonDocument and add the  to RSS collection

            //To get a particular record
            //var filter = new BsonDocument("QuestionID", rssFeed.QuestionID);
            //var list = collection.Find(filter).ToList();

            return rssCollection;
        }
    }
}

The is self explanatory and henceforth no further explanation is provided.

Now let us look at how we are loading the RSS feeds. We have a class call RSSReader.cs which has the below code

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;

namespace ConsoleApplication1
{
    public class RSSReader
    {
        public IEnumerable<RSS> GetRSSFeeds()
        {           
            string url = <RSS FEED URL>;            
            SyndicationFeed feed = null;

            using (var reader = XmlReader.Create(url))
            {
                feed = SyndicationFeed.Load(reader);
                var syndicationItems = feed.Items.ToArray();

                for (int i = 0; i < syndicationItems.Length; i++)
                {
                    yield return new RSS
                    {
                        Id = System.Guid.NewGuid(),
                        QuestionID = syndicationItems[i].Id,
                        QuestionTitle = syndicationItems[i].Title.Text,
                        QuestionDescription = syndicationItems[i].Summary.Text,
                        PublishDate = syndicationItems[i].PublishDate.Date
                    };
                }
            }            
        }
    }
}

As can be figure out that, we are using SyndicationFeed Class that represents a top-level feed object, <feed> in Atom 1.0 and <rss> in RSS 2.0. It resides under System.ServiceModel.Syndication namespace.We are using yield statement for maintaining a stateful iteration.

And finally, the main invocation program.

using System;
using System.Collections.Generic;
using static System.Console; //please note the use of "using static" of C#6.0
using static System.Threading.Tasks.Parallel; //please note the use of "using static" of C#6.0

namespace ConsoleApplication1
{
    class Program
    {
        static List<Guid> lstIDs = new List<Guid>();
        static void Main(string[] args)
        {            
            InsertRSSFeed();
            SelectRssFeed();
            UpdateRssFeed();
            DeleteRssFeed();
        }        

        #region CRUD Operations for MongoDB

        private static void InsertRSSFeed()
        {
            ForEach //please note the use of Parallel.ForEach
                (new RSSReader().GetRSSFeeds()
                , feed => lstIDs.Add(new MongoDBOperation().InsertRssFeed(feed)));
        }

        private static void SelectRssFeed()
        {
            new MongoDBOperation()
                 .ReadRssFeed()
                 .ForEach
                 (
                    i =>
                        WriteLine
                        (
                            i.Id + Environment.NewLine +
                            i.QuestionID + Environment.NewLine +
                            i.QuestionTitle + Environment.NewLine +
                            i.QuestionDescription + Environment.NewLine +
                            i.PublishDate + Environment.NewLine +
                            "----------------------------------------------------------------------------"
                        )
                 );
            ReadKey();
        }

        private static void UpdateRssFeed()
        {
            var rssFeed = new MongoDBOperation()
                            .ReadRssFeed()[0];
           var updatedResult = new MongoDBOperation().UpdateRssFeed(rssFeed);

            WriteLine("---------------------------------OLD ITEM--------------------------------------");
            string strOldRSS = $"Id: {updatedResult.Item1.Id}";
            strOldRSS += $",QuestionID: {updatedResult.Item1.QuestionID} ,QuestionTitle: {updatedResult.Item1.QuestionTitle} ";
            strOldRSS += $",QuestionDescription: {updatedResult.Item1.QuestionDescription} , PublishDate: {updatedResult.Item1.PublishDate}";
            WriteLine(strOldRSS);
            WriteLine(Environment.NewLine);

            WriteLine("-----------------------------------MODIFIED ITEM-------------------------------");
            string strModifiedRSS = $"Id: {updatedResult.Item2.Id}";
            strModifiedRSS += $",QuestionID: {updatedResult.Item2.QuestionID} ,QuestionTitle: {updatedResult.Item2.QuestionTitle} ";
            strModifiedRSS += $",QuestionDescription: {updatedResult.Item2.QuestionDescription} , PublishDate: {updatedResult.Item2.PublishDate}";
            WriteLine(strModifiedRSS);
            WriteLine("----------------------------------------------------------------------------");

            ReadKey();
        }

        private static void DeleteRssFeed()
        {
            var rssFeeds = new MongoDBOperation().ReadRssFeed();
            WriteLine("Before Delete => Record Count = " + rssFeeds.Count);           

            var items = new MongoDBOperation().DeleteRssFeed(rssFeeds);

            WriteLine("----------------------------------------------------------------------------");
            WriteLine("After Delete => Record Count = " + items.Count);           

            ReadKey();
        }

        #endregion

    }
}

The program is pretty simple to understand. But before executing the program we need to ensure that MongoDB Server is up and running. To do so, we need to issue the below command

C:\mongodb\bin>mongod.exe

And we can perform the various activities of CRUD. However, if we need to check from the activities from the MongoDB command prompt, we can issue the below commands

References

Getting Started with MongoDB (C# Edition)

Conclusion

In this article, we have learnt about the appropriate use of a NOSql Document Database by performing CRUD operation using MongoDB taking RSS Feed as an example. Hope this will be useful. Zipped code attached.

N.B.~All the CRUD operations demonstrated here is purely synchronous way. In some other article we will look into doing the same by asynchronous way.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)