Tag: mongodb

MongoDB and the rest of NoSQL solutions

Posted by – August 8, 2009

In the last weeks I have been evaluating two NoSQL solutions available in the market, MongoDB and CouchDB, each solution has very interesting features, but besides of its features we must consider how much easy to use each one is, right?

Let’s talk a little about CouchDB first, CouchDB is a document-oriented solution written in Erlang language, with CouchDB you can easily persist documents in JSON format it also has a very powerful RESTful interface from where you can retrieve database information and persist/query documents.

But how to perform basic operations on CouchDB like persist or query data? It’s simple, you just need to know how to use its RESTful interface, the example below shows how much easy is persist data in CouchDB with help of curl tool:

1
curl -X PUT -d '{"name": "Rogerio Araujo", "address": "Estrada dos Bandeirantes", "city": "Rio de Janeiro", "state": "RJ", "country": "Brazil" }' "http://127.0.0.1:5984/contacts/user3"

The command above will create the following JSON document on database:

1
{ "_id": "user3", "_rev": "3771096703", "name": "Rogerio Araujo", "address": "Estrada dos Bandeirantes", "city": "Rio de Janeiro", "state": "RJ", "country": "Brazil" }

Once we have few documents stored on CouchDB we need to create a view used to help on query data, to do it, you must write the following javascript function and store it as a document on database:

1
2
3
4
function(doc) {
  if(doc.name)
     emit(doc.name, doc);
}

More

Share

Working with Mongo Wire Protocol on C#

Posted by – August 2, 2009

Last saturday I started a small C# project when I try to send a message to MongoDB, after few hours working on it I finally could see the database receiving the message sucessfully.

Here’s the code that I used to send the message to database:

Program.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace Mongo.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a socket connection to mongodb instance
            Socket socket = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(Dns.GetHostAddresses("192.168.0.103"), 27017);

            //This is the message to be sent to mongodb instance
            List<byte> message = new List<byte>();
            message.AddRange(Encoding.UTF8.GetBytes("This is a test."));
            message.Add(0x00);

            //Each message to be sent to mongodb must have a header like
            //where we can specify which operation will be performed on server.
            List<byte> header = new List<byte>();
            header.AddRange(BitConverter.GetBytes(16 + message.Count));
            header.AddRange(BitConverter.GetBytes(1));
            header.AddRange(BitConverter.GetBytes(0));
            header.AddRange(BitConverter.GetBytes(1000));

            //Now we must put the message header a body together before send it
            // to the server.
            List<ArraySegment<byte>> buffer = new List<ArraySegment<byte>>();
            buffer.Add(new ArraySegment<byte>(header.ToArray()));
            buffer.Add(new ArraySegment<byte>(message.ToArray()));

            //Send the message to the server
            socket.Send(buffer);

            //Close connection
            socket.Close();
        }
    }

A very good documentation about how to write a MongoDB driver can be found here.

Share

Getting MongoDB running on Solaris 10

Posted by – July 20, 2009

That’s what I did to get MongoDB running on Solaris 10:

1. Download, compile and install gcc 4.4

MongoDB can’t be compiled with bundled version of gcc on Solaris 10, it requires gcc 4.0 at least, I couldn’t find the binary package of gcc 4 for Solaris 10 anywhere on the web, so I decided to download and compile gcc 4 by myself.

You may get some build errors because of missing targets during build process, try run “make -i” to ignore build errors.

2. Download, compile and install SpiderMonkey 1.8.0 RC 1

Another MongoDB dependency is SpiderMonkey, MongoDB uses SpiderMonkey to process JSON data, please download and uncompress SpiderMonkey sources.

Once the tarball is successfully downloaded, you must make a copy of makefile for Solaris 10 for Inter PCs by running the following command:

1
cp config/SunOS5.10.mk config/SunOS5.10_i86pc.mk

And set the PATH with the directory that contains the ar tool:

1
export PATH=$PATH:/usr/ccs/bin

Now run the following command fron src directory:

1
gmake -f Makefile.ref

More

Share