Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Friday, February 4, 2011

MongoDB Error

0 comments
If you happen to pass by this error

[02:33:05] mongodb$ mongo
MongoDB shell version: 1.6.3
connecting to: test
Sat Nov 13 02:33:10 Error: couldn't connect to server 127.0.0.1 (anon):1137
exception: connect failed


Check if your mongo is running by issuing "mongod" command. If you get something like this,

Sat Nov 13 02:30:41 MongoDB starting : pid=294 port=27017 dbpath=/data/db/ 64-bit
Sat Nov 13 02:30:41 db version v1.6.3, pdfile version 4.5
Sat Nov 13 02:30:41 git version: 278bd2ac2f2efbee556f32c13c1b6803224d1c01
Sat Nov 13 02:30:41 sys info: Darwin erh2.10gen.cc 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40
Sat Nov 13 02:30:41 exception in initAndListen std::exception: dbpath (/data/db/) does not exist, terminating
Sat Nov 13 02:30:41 dbexit:

Sat Nov 13 02:30:41 shutdown: going to close listening sockets...
Sat Nov 13 02:30:41 shutdown: going to flush oplog...
Sat Nov 13 02:30:41 shutdown: going to close sockets...
Sat Nov 13 02:30:41 shutdown: waiting for fs preallocator...
Sat Nov 13 02:30:41 shutdown: closing all files...
Sat Nov 13 02:30:41 closeAllFiles() finished

Sat Nov 13 02:30:41 dbexit: really exiting now


You can see a lot of things to do here but you should probably check on your log file to see some errors while your mongo wouldn't start. When I tailed my logs, I saw a different error stated like so

**************
old lock file: /usr/local/mongodb/data/mongod.lock. probably means unclean shutdown
recommend removing file and running --repair
see: http://dochub.mongodb.org/core/repair for more information
**************


And yes I did came from a crash. So I checked and removed the file and it automatically ran again based on my existing tail on the logs and it was back again.

Hope this helps somebody else as well.

Friday, September 17, 2010

Ramaze and Mongo(DB and mapper): Day 2

1 comments
I've read on most documents before, that when using the NoSQL world it is still better to use it with RDBMS. Correct me if I'm wrong with this one, is it better to use them together or use them apart. If you have documents or links discussing this one, leave them in the comments, I'd love to read them. Also if so,
I'd like to know more is how should I separate them? Like what tables/collections would/should be this NoSQL or SQL?

So I tried testing with MongoDB and Sequel to make them play together and it was quite easy. If you have been using Ramaze before with Sequel, just do the same thing as before then just add configurations for the Mongo (which I also forgot to write configurations on my first part).

Configuration
with both MongoDB and Sequel
require 'rubygems'
require 'ramaze'
require 'yaml'

require 'bson'
require 'mongo_mapper'
require 'sequel'
require 'erubis'

MongoMapper.database = 'mongoplay'

Ramaze.options.layouts = '/view/layout'
Ramaze.options.mode = :dev

Ramaze.options.roots = [__DIR__] # Make sure that Ramaze knows where you are

Sequel.mysql(
  'mongoplay',
  :user => 'uname',
  :password => 'pword',
  :host => localhost
)

# Acquire controllers and models and other utilities
Ramaze.acquire 'util/*'
Ramaze.acquire 'model/base.rb', 'model/*'
Ramaze.acquire 'controller/base.rb', 'controller/*'
Ramaze.acquire 'config/routes.rb'


In the previous post on this topic, I was using a messaging/posting system and in here, I added a users table on a MySQL so we could tie some posts to a user if ever they wanted to register (I won't discuss here how to register/authenticate the user for now but you could use this link if so ever, that's what I used before). So the same model would apply ordinarily. I was using the user helper here.

User model
class User < Sequel::Model
  def self.register(email, password)
    User.create :email => email, :password => encrypt(password, salt), :salt => Digest::SHA1.hexdigest("--#{Time.now.to_f}--#{email}--")
  end

...


And now to insert posts and tie them to a certain user. You can also define the key in the message model or don't and you'd still insert them the same like so:
...
  if logged_in?
    Message.create :name => user.username, :body => request[:body], :user_id => user.id
  else
    Message.create :name => request[:name], :body => request[:body]
  end
...

And I just basically did the same with the comments section.

Now, now, to pull all the messages that a certain user has made:
class UsersController < Ramaze::Controller
  map '/users'

  def index(id)
    @msgs = Message.all :user_id => id.to_i
  end
end

If ever you were using a different model, just query first in the SQL table then query in the Mongo or vice-versa. :D

Wednesday, September 8, 2010

Ramaze and Mongo(DB and mapper): Day 1

0 comments
Edit ** I forgot to mention here how you would write a configuration file but you can check out my next post which mentions a small config for mongoDB and also for Sequel.

Okay. So day 1. I've chosen to use MongoMapper for now over the other two I've provided last post. MongoMapper is quite good, I haven't read/delved into their code but he said it was readable so looking forward to it. I just hope they don't make MongoMapper too rails-specific.

Okay, so moving on. I've started with the basics of most apps that is "Inserting data" on a "Collection". And having a "One to Many" relationship. This is just a basic app where you could post and comment to demonstrate. I assume here that you already have working applications and just want to start out using MongoDB.

The model would look like this.
Message Model
class Message
  include MongoMapper::Document

  key :name, String, :require => true
  key :body, String, :require => true
  timestamps! # This would create a "created_at" and "updated_at" keys on create

  many :comments
end

Take note that I have defined keys in the model. The keys can have typecast but it doesn't require you to have one. Also, you need not to define every key in the model as documents are schema-less, that's also one of the reasons of using MongoDB (unkown unknowns).

So for example you have a key :blah.
Test Model
class Test
  include MongoMapper::Document

  key :blah
end
Test.create :blah => 42
# => { "_id" : ObjectId("..."), "blah" : 42 }

Test.create :blah => "the answer"
# => { "_id" : ObjectId("..."), "blah" : "the answer" }

Test.create :blah => 42, :answer => "ultimate question"
# => { "_id" : ObjectId("..."), "blah" : 42, "answer" => "ultimate question" }

Okay. Going back to inserting records (but I think I've shown that already in the test). I only have basic information for now but here is a snippet of posting.
  def post
    if request.post?
      Message.create :name => request[:name], :body => request[:body]
      redirect r(:/)
    end
  end

Upon posting a message here, the data would look something like this.
{ "_id" : ObjectId("..."), "name" : "Post 2", "created_at" : "Tue Sep 07 2010 18:31:45 GMT 0800 (PHT)", "body" : "Test Body!", "updated_at" : "Tue Sep 07 2010 18:31:45 GMT 0800 (PHT)" }
Now I'd like to step on to relations, embedded documents.
Did you notice the last line before ending the MessageModel? It is a many relationship to comments. A comment model would look like this:
Comment Model
class Comment
  include MongoMapper::EmbeddedDocument

  key :name, String, :require => true
  key :body, String, :require => true
end

Note on the line 2 EmbeddedDocument would embed this document on the Message document. To try it out:
Per post snippet
  def posts(id = nil)
    id = id || request[:id]
    redirect r(:/) unless @msg = Message.find(id)

    if request.post?
      @msg.comments << Comment.new(:name => request[:name], :body => request[:body])
      @msg.save
    end
  end

This would produce a record like this
{ "_id" : ObjectId("4c8618b6be9294085d000001"), "name" : "Post 3", "created_at" : "Tue Sep 07 2010 18:49:26 GMT 0800 (PHT)", "comments" : [
    {
        "name" : "Tester",
        "body" : "Comment Yah!",
        "_id" : ObjectId("4c862781be929409d100000a")
    }
], "body" : "Another test body!", "updated_at" : "Tue Sep 07 2010 19:52:33 GMT 0800 (PHT)" }

Then when you pull the data from the "Per post snippet". You could iterate on the comments like so:
  <% @msg.comments.each do |c| %>
    <%= c.name %><br />
    <%= c.body %><br />
  <% end %>

This app and idea is far from perfect as it would still require validations and some form of filter to remove scripts but you get the gist of it. Also, I didn't include the views as I haven't written them completely as well, they are just basic forms with inputs and textareas and iterations for display but I think you all get what they would look like. If you need to see it, post a comment and I'll email it to you.

I don't know if I have written it awesome for now but I would like to hear how others are doing with Ramaze with MongoDB and MongoMapper.

Also, here is a complete code of the controller:
class MessagesController < Ramaze::Controller
  map '/'

  def index
    @msgs = Message.all
  end

  def post
    if request.post?
      Message.create :name => request[:name], :body => request[:body]
      redirect r(:/)
    end
  end

  def posts(id = nil)
    id = id || request[:id]
    redirect r(:/) unless @msg = Message.find(id)

    if request.post?
      @msg.comments << Comment.new(:name => request[:name], :body => request[:body])
      @msg.save
    end
  end
end

Monday, September 6, 2010

MongoDB start!

0 comments
I'll be playing around with MongoDB in the coming years and I think changing to this schema-less style works.

Installation


I've followed this link to install MongoDB on Snow Leopard. If you happen to get some errors here, make sure that all paths stated in the that document are created or else you'll get nasty errors. I was trying to mimic the errors I had before but I can't seem to get them again. Anyway, afterward, install the ruby driver to use here.

And now I'm looking at some mappers to use. I'm looking at MongoMapper or Candy or looking also at mongomatic while looking at tutorials and documents to play with mongoDB.

If you guys have any suggestions to help me out, please post on the comments.

Monday, August 23, 2010

A taste of Mongo

0 comments
I'm having a lot of things to do lately. Lots of projects I'm thinking of doing, on top of projects I really have to do, and that is aside from my day job, Traxier (which I/we have to focus on for now because hard launch is approaching, early September this year I think).

So aside from Traxier, I'm trying to finish up alpha stage of SureScene as I want to get surescene something to do aside from just posting and sharing events and scenes.
I also have to learn design, webdesign. I have to come up with a nice design for a media site my friends and my brother are wanting their own site. Haha! I even bought a mouse to use just for designing (or maybe for playing SC2 when I get the chance to buy the game). :P Well layout-ing is easy as I've already got the taste of HTML and CSS, but playing with what color or image or design to do is harder than I thought, my designs always end up bland for my taste. XD

I also want to try and play with MongoDB right now. I'm trying to think of a simple app that could use the MongoDB so I could get a taste of how it works, and also get a feel of how NoSQL (schema-free / Document oriented DB's) works. I've already read and heard many things about it and just started installing it on Ayie.
Maybe after a few tries of MongoDB and getting the hang of what should be RDBMS and not in an application. I'll try implementing it on SureScene and see what it can do and maybe I'll get more involved in it.

And now I also want to try out HTML5's websocket, I bet this will change a lot for webdevelopment which was "usually" just one way.