Showing posts with label mongoDb. Show all posts
Showing posts with label mongoDb. Show all posts

Friday, July 8, 2011

Progress Report P.2

Printing out the blog entries.  I dumped a bunch of blog entries I grabbed off of blogger to use as test data. This is what my vacation entry looks like:


Here is the code I'm using to display it:

Code To Show All the Blog Entries

Right away I pull up the blogs db. then grab all the entries as a Collection. Next I opened a cursor and to the Collection iterated over it.

A couple key points:
  •  I sorted the entries by date (dt) in descending order. 
      • Its a 2 step process. 
        1. Get The Collection
        2. Sort The Results

                             This is different than SQL where I would say something like:
                            SELECT * FROM BLOG ORDER BY DT DESC;
  • The Blog Entry is formatted using Text_Wiki which requires a call to                  require_once("Text/Wiki.php");
           at the beginning of the page. The $wiki object allows me to add links, lists and other formatting  easily in the blog entries. It would be great to add a nice WYSIWYG entry system like blogger uses but I can get a lot of mileage out of wiki text so its a low priority item to upgrade.

So that's where I'm at. Still need lots of features but I'm having fun with it.

Til next time
Cheers,
TheSortedProgrammer

Progress Report

I have a basic blogging site using mongodb up and running. Horah!

Basic Features:
  • Input Page without formatting
  • Blog Output Page with formatting
The Blog Entries consist of these fields:
  • User
  • Entry Title
  • Blog Entry
  • Labels
  • Date
So that's where the Collection design is at. I still need to work in comments.

So how did I get here. Let's look at some code. The first page to look at is the post-creater page.
The very modest looking page right now:

Blog Input Page
<html>
<body>
<?php
 if(isset($_POST["start"]) == false){
  $_POST["start"] = "y";
 }    
echo '<br/><p> Welcome To The Overenginered Blogger Post Creator</p> <hr\>'; 
?>

<?php
    if($_POST["start"] === "n"){       
            try{         
                date_default_timezone_set( 'America/New_York' );                             
                $conn = new Mongo();
                $db = $conn->selectDB("blog");
                $collection = $db->items;
                $item =array(
                    'title' => $_POST['title'],
                    'txt' => $_POST['txt'],
                    'labels' => $_POST['labels'],
                    'user' => $_POST['user'],
                    'dt' => new MongoDate( strtotime( date( 'Y-m-d H:i:s')))
                );
                $collection->insert($item);

                /// disconnect from server
                $conn->close();
            } catch ( MongoConnectionException $e ) {
                echo '<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';
                exit();
            }
    }
?>

  <form action="postCreator.php" method="post">Enter a post:<br/>Title:<input type="text" name="title" /><br/><textarea name="txt" cols='88' rows="12"/><br/>Labels: <input type="text" name="labels" /><br/>User: <input type="text" name="user" /><input type="hidden" name='start' value='n' /><input type="hidden" name='action' value='i' /><input type="submit" value="Blog"/></form>
</body>
</html>

The key parts of the page are:
  1. The first few lines which checks for the POST parameter 'start' 
  2. The large chunk of code in the middle of the page is where I am connecting to mongodb and inserting a new record.
  3. The form at the bottom of the page presents a form to for user to enter a new blog entry.
The first part of the code has nothing to do with mongodb. It just handles the situation when the required argument 'start' isn't passed in by creating and setting it to 'y'. This situation happens when you first go to the page and the parameter hasn't been set yet. Once you use the form and hit 'submit' you send 'start'='n' to tell the page that you want to insert a new entry.

The second part is where the meat of the code is. I connect to the mongoDB
                $conn = new Mongo(); // connect
                $db = $conn->selectDB("blog");
                $collection = $db->items;

Then I insert the entry using the elements passed in using the form at the bottom of the page.

                 $item =array(
                    'title' => $_POST['title'],
                    'txt' => $_POST['txt'],
                    'labels' => $_POST['labels'],
                    'user' => $_POST['user'],
                    'dt' => new MongoDate(strtotime(date('Y-m-d H:i:s')))

                );

The trickiest part for me was inserting the date. My real issue was that I didn't take enough time checking out all the documentation so I missed  PHP Mongodate Class. Everything went much smoother once I found that class.  I'm so used to Oracle where I can do something like
insert into table (date_column) 
values to_date('01-Jan-2010','DD-Mon-YYYY');

So when I first hit the point I needed to put in a date I just slapped in some code:

'dt' => date('j-m-y h-i-s')
 
The problem was that this put in a String instead of a Date. This turned out to be a big issue when I went to pull up Blog Entries because I needed to pull them up by date in descending order.

DESIGN FLAW #1 - WRONG DATE TYPE

By using the MongoDate class I can now sort Blog Entries by date.

Finally,  the form is present to allow data entry. Its not a WYSIWYG editor (obviously). Right now I'm going with a WIKI Text Markup PEAR module to help bridge the gap. (More on that latter). This is a long post already so I'll save the Blog Output page for next time.

Until Then
Cheers,
TheSortedProgrammer

Thursday, June 16, 2011

Optimizing MongoDb talk

There are a lot of good presentations from MongoNYC 2011 Conference. One of them that you can check out the slides from is: Optimizing MongoDb by Andrew Rollins. 




  One of the things you have to get used to about NoSQL is that its such a new topic that there aren't many resources out there for there - O'Reilly books, classes, etc. Its getting better all the time but its still a subject in its infancy. One great source of information you can tap into a couple times a year are the conferences that are put on for NoSQL. In fact, my first real exposure to the technology was at the NoSql East 2009 Conference. I highly recommend checking out the site for - its still got logs of relevant videos and slideshows showing how some of the biggest companies (Twitter, Facebook, Microsoft, etc.) are using NoSql to handle their needs.

Cheers,
TheSortedProgrammer

Tuesday, June 14, 2011

Mongo Db

I started working on a new pet project -  creating a blogging system using PHP and mongoDb. I have a keen interest in NoSQL and I've been wanting to get my hands dirty with mongoDb for sometime now and this seems like as good a project as any to put the db through its paces.

I thought I would document the process here until my new mongo powered blog is working.

So far so good - I installed the mongoDb with no problem and went through the start-up guide on the mongoDb site.

Next I installed the php half of the equation - MongoDB database driver for php.
Unfortunately I didn't have pecl installed on my new MacBook Pro -  Curses A Road Block!
Running the commands:
curl http://pear.php.net/go-pear.phar > go-pear.php
php -d detect_unicode=0 go-pear.phar
got me the pear/pecl packages I needed.

Now I just ran:
pecl install mongo
and boom I had the drivers installed. All that was left was to update the php.ini file and restart Apache.

As a final step I created 2 pages.
postCreator.php - a page with a form that allows the user to create a new blog entry and insert it (blog it) into their collection. The page creates mongo records that consist of a few fields (username, timestamp, title, text, label, etc.)
view.php - a page which displays the blog entries.

So thats where I've gotten to. All matters of formatting and style sheets have to be worked out. As well as a commenting system and lots of other issues. Still, I'm impressed by how quickly I was able to get up and running with mongoDb.

Cheers,
TheSortedProgrammer