Archive | Mongodb RSS for this section

Management studio for Mongodb, -Mongodb Part 3

I like command prompts, and CLI’s at times, perhaps you too like them. But most of the time I love to visualize stuff. For example, how does mongodb databases, collections and documents look visually? What if we can have a quicker simple way to visually work and manage our mongodb.

MSSQl is to Sql Server Management studio as MySQL is to phpmyadmin (if I am not wrong :)), what about about mongodb..?

Welcome Robomongo, which is a shell-centric cross-platform MongoDB management tool. You can download and install Robomongo from there website here, select your platform, download and basically go through the installation steps easily.

Some of the features highlighted from the websites includes:

1. Robomongo embeds or rather uses same javascript engine that powers mongodb’s v2.2 shell, hence you can reuse your existing skills.

2. You can open as many shells as you need pointing to a single mongoDB database or many shells for many different databases.

3. Your code is executed statement by statement way.

4. You get autocompletion for all objects including functions well known by javascript runtime, including autocompletion for databases, collections and even your document.

Once you are finished installing, ensure your mongodb instance is running. Follow the steps the here to start mongod shell, once you are done take not of the port (default is 27017).

Then double robomongo to open it, and you will see the following screen:

robo_mongo_1

From the screen you can see there is not connection, and so click on “create” link on the small child windows, and this present you with the following screen.

robo_mongo_2

Provide a preffered name for you connection, in my case I gave it ‘Connection One’. Ensure that the address points to the server where you mongod instance is running and also the port is correct. In my case I am using local instance running on port 27017.

You can test the connection by clicking the ‘Test’ button, which should tell you if the connection succeeded or not. In my case I got a success like below.

robo_mongo_3

For now I don’t have authorization to my mongodb, just click ‘close’, and then “save”, make sure your screen at this point is similar to this one:

robo_mongo_4

You can see our connection is listed. At this point select “Connection One” and click on the “Connect” button. If you have done everything right then your screen should look like:

robo_mongo_5

 

Voila! Here is you visual representation of you mongod instance, databases, collections, functions, documents etc. In my case I expanded to see how documents are represented as belowrobo_mongo_6

So now you know! If you want to explore robomongo, just right click on any level node on the tree, and see what you can achieve. But I can assure you can create, update, list and delete documents. You can also createa database here, add table functions which are just but javascript functions. There will be more advanced posts to come and so keep watch!!

Again happy coding nerdizzles 😀 !!

 

Advertisements

MongoDb Databases, Collections and Documents Explained – MongoDb Part 2

Welcome back for another post as promised on more about mongodb. If you missed part one of the series check it out here

The objectives of this post include.

  1. Explain mongodb in relation to a relational database for a better understanding
  2. What is a mongo database
  3. What are mongodb collections
  4. How to create a collection
  5. What are documents?
  6. How to create a document

Explain mongodb in relation to a relational database for a better understanding.

Being from a relational database, its of great importance to set some of the aspect clear. In a relational database, data is organized in records, in a database table. Basically in RDMS we have tables and rows.

In mongodb or nosql database, the data is structured in collections and documents. In mongodb the documents are just but JSON which is simply a key-value pair fields.JSON is also a natural data format for use in the application layer. JSON supports a richer and more flexible data structure than tables made up of columns and rows. In addition to supporting field types like number, string, Boolean, etc., JSON fields can be arrays or nested sub-objects. This means we can represent a set of sophisticated relations which are a closer representation of the objects our applications work with.

Creating a mongo database.

You can create a database even without using it. Simply use the following command on CLI

    use accounts

This is illustrated in the following snapshot
Creating_db

By doing this we have created a database, but remember that mongo will not create the database until you create a collection in it.

Collections.

Mongodb collection are the containers that holds the documents. Mongodb creates a collection implicitly when you first reference  a collection in a command. In the following figure I am going to connect to my mongo instance, and show you what I mean by this.

Implicitly create a collection

I have tried to be explicit as possible in describing the above image.Of importance is step 4 where we reference unexisting student collections and insert a document, and mongo creates students collection automatically.

There are other collections called capped collection. By definition from mongo documentation, this is a fixed sized collection that automatically overwrites its oldest entries when it reaches its maximum size. This sort of collection can easily be created with createCollection

  db.createCollection(name, options)

The options parameter is optional and its document (json) in the following format

{ capped: <boolean>,
  autoIndexId: <boolean>,
  size: <number>,
  max: <number>,
  storageEngine: <document>
}

Of importance here is the ‘capped’ boolean, which indicates that the collection is capped. If this is set to true then ‘size’ is also required to be provided. In the following figure I am creating a capped collection called “Subjects”
Capture_1

Simply those are the few and quick ways to creating collections for mongo.

Documents.

When you create a collection implicitly mongodb does not create the collection per se. For the collection  to be available you need to insert a document into it. Documents are the just json objects (key-value pairs) which are contained in a collection.

Mongodb has various methods to adding new documents to a collection.

One of the method is db.collection.insert();

 db.members.insert(
   {
     name: "name1",
     accounts: [ { name: "Savings", balance: 25 }, { name: "Fixed", balance: 50 } ],
     category: "junior"
   }
)

The above operation return, showing how many documents have been inserted..

   WriteResult({ "nInserted" : 1 })

Also a quick note is that you can pass a collection to insert and it will insert all the  documents, but will return the following result on success. We will discuss this and bulk insert in a future post. 🙂

BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

The other option to adding documents to our collection is using db.collection.save(). This operation updates an existing document or inserts a new document. If the document passed to this operation has ‘_id’ parameter then result would be an update by getting the document with the ‘_id’ and replacing all fields in the existing record with the fields in the document, and if the document does not exist with the specified ‘_id’ it perfoms an insert. The write concern can be taken as being synomous more to isolation levels

db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)

Conclusion

There is alot to write about this topic, but lets end it here for now. There will be more on future posts. In this post we have been able to cover on how to create mongo database, collections and documents. We also quickly and simply differentiated on ‘save’ and ‘insert’ operations for documents, so you know how , when and where to use them.

Feel free to drop a comment or drop an email with your concerns and views on the topic.

Stay tuned for part 3 -Happy coding!! 🙂

Tired of clicking to your mongodb installation folder to spin it up!! Never again! Setting path for mongodb

Long title huh? Leave it alone just dive into the content then you will get the meaning of it 🙂

Mongodb is one of the fastest evolving nosql databases. And in an effort to learn it, I have found myself so stuck into it. It sometimes come with it own developer challenges. And in this series of posts to follow, I will be highlighting what I have and will find interesting and worthy sharing with you.

My assumption in these posts is and will be that you have downloaded mongodb here and installed it your machine as per the well documented installation guide.

In my case, I did that on my box running windows 8.1, and my installation path was “C:\Program Files\MongoDB\Server\3.0\bin”. At first I was very excited to run “cd” or better “pushd” to this directory to spin up my mongod.exe instance like show:

How to start manually

After a few times of doing this, it became kind of boring and I started to research on a better way to do this.  The simplest solution that I found was to add the mongodb bin installation path (“C:\Program Files\MongoDB\Server\3.0\bin”) to the system environment path variable. So copy the path to your clipboard, the go my computer and follow the step as highlighted below.

NB: Assumption here is that you are on windows 8.1/8. If not find out how to get to the “Advanced system settings” in your operating system.

add_path

Once you hit step 4, double click and you will definitely get a small pop up window, which allows you to edit the “Path” variable. You might find other paths there, seperated with semi-colon. You simply append the path on your clipboard, and make sure you separate with semi-colon as well. After successfully pasting your path and made sure it looks like I have highlighted in yellow, just click ok to all the opened windows.

And with that in place, you can simply open any command prompt, including “cmd” and type any mongodb command e.g mongod.exe, mongo.exe etc like show below.  You will need to close any opened prompts to achieve get the effects, by this I mean for any opened prompt the following will not work, you will need to close it.

Updated_screen

You can see now, I don’t have to navigate to the installation directory to spin up mongod instance. Just from my command prompt, I can now run mongod.exe.

One of the reasons, I found this relevant to share is because there are alot of posts to follow regarding mongodb, and we will always be referring to this post for spinning up our mongodb instance.

Happy coding and keep an eye for the next post soon!

 

%d bloggers like this: