WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Friday, August 16, 2013

Spray.io REST service - MongoDB - Lesson 4

View the lessons list at https://github.com/karthik20522/SprayLearning

Now that we have Spray Service Setup and Routes defined, we can now hookup the database to add and fetch customer information. For the database I am using MongoDB and Casbah. Casbah is a Scala toolkit for MongoDB. Casbah is a "toolkit" rather than "driver", as Casbah is a layer on top of the official mongo-java-driver for better integration with Scala.

To get Casbah setup, we need to add casbah and it's related dependencies to the Build.scala file.
Note that we have slf4j and scala logging in the dependencies. Without slf4j you would get "Failed to load class org.slf4j.impl.StaticLoggerBinder" error.

In my example, I have created a MongoFactory that has 3 functions: getConnection, getCollection and closeConnection.
Now that we have our Factory method, the next is building the data access class for inserting and fetching data. Following code snippet has 2 operations:
  • saveCustomer - which returns back the GUID after inserting into MongoDB
  • findCustomer - find customer by GUID
Now to integrate this to the service: More information and resources on Casbah:

Labels: , ,

Thursday, May 17, 2012

Mongodb - not so common basics - Part 2



Part 1 of this article: MongoDB Not So Common Basics - Part 1

Labels:

Wednesday, February 15, 2012

MongoCSharp - Custom Serializer

As part of MongoDB Csharp driver is the custom serialization property, IbsonSerializable. The idea behind the use of this is to create a custom seralizer and deserializer to add and access documents. But as always the documentation is quite off and hard to find any information about this. Following is an example of using bson serializer:

Let’s say that we want to build a class that has a “LogDate” property of type DateTime but we want to save this property into MongoDB as “CreateDate” alias and of type System.Int64.



In the Serialize function, an anonymouse class is created where CreateDate is the new field Name and with value to be long (DateTime.Ticks). This anonymouse class is then converted into a BsonDocument and sent into the writer.

In the Deserialize function, the data from MongoDb is read from the reader and converted into a DemoClass object. CreateDate is read from the reader and converted into logDate format of DateTime.

Labels:

Tuesday, February 14, 2012

Mongodb - not so common basics

Basics of MongoDB:

* _id field is automatically indexed by MongoDB

* Maximum document size is 16MB

* Maximum size of a index field value is 800 bytes i.e. if a value who’s size/length is greater than 800bytes long, would be skipped from indexing. As an FYI, in SQL it’s 900 bytes.

* Searching against a non-indexed field is a no-no. This would be extremely slow and would require mongo to load all documents in memory to search. Note that MongoDB is fast as it tries to load maximum amount of data into memory.

* For a MongoDB to use Index the search key must be in order of text / beginning char of the index key

* A substring search of a Indexed field would not use the index. So a regex operation would be slow, since index’s wouldn’t be used.

* Index searches are case sensitive. Doing {/[value]/i} – non-case sensitive search would be slow

*MongoDB uses paged memory mapped.

*MongoDB creates index’s in background. Adding a new index’s though might take long (depending on size of collection) but also doesn’t cause Mongo to choke much. Be brave while adding new Index’s

* Use Explain command to get a quick overview of the query processing. Example:





* Remove one array item from a property, in one specific document:


- Remove all instances of a property, which happens to be an array, from all documents:


* MongoDB has a rest interface to monitor it’s status. It’s usually on 28017 port. - - rest is the command to enable rest interface.

* mongod is basically Mongo Daemon.

*MongoCSharp uses connection polling when using MongoServer.Create(). Do not programmatically disconnect the server as the open connection would be reused.

* When using MongoCursor, cursor.Count() can be used to get the total results when using limit and filter commands.

* For Windows users, MongoVUE is a pretty decent tool for visual interface.

* MongoDB monitoring is needed as like every other database. MMS (mongo monitoring service) is a good starting point. MMS is a free online agent provided by 10gen (MongoDB parent company)

* For a more speedier real time metrics interface, MongoLive chrome plugin is a good start.

Labels: