Showing posts with label Mongodb. Show all posts
Showing posts with label Mongodb. Show all posts

Wednesday, 29 August 2012

Create a New app Using Mongo db



create new repository using this command:

$ rails new <app_name> -T -O

Use the -T -O flags to skip Test::Unit files and Active Record files.

Ex: $ rails new example_mongo -T -O

include this gems in your gem file:


gem "mongoid", "~> 2.2"    
gem "mongo", "~> 1.5.0.rc0"
gem "bson", "~> 1.5.0.rc0"
gem "bson_ext", "~> 1.5.0.rc0"

go to the repository and run $ bundle update command.

then run $ rails g mongoid:config to generate your configuration file.

try to create a table using scaffold

exp: $ rails g scaffold user name:string email:string

Every thing should go fine... That's it..
:)




Mongoid::Paperclip


Mongoid::Paperclip - Making Paperclip play nice with Mongoid 

1st Install Image Magick using

$ sudo apt-get install imagemagick

As the title suggests: Mongoid::Paperclip makes it easy to hook up Paperclip with Mongoid.
This is actually easier and faster to set up than when using Paperclip and the ActiveRecord ORM. This example assumes you are using Ruby on Rails 3 and Bundler. However it doesn't require either.

Setting it up

Simply define the mongoid-paperclip gem inside your Gemfile. Additionally, you can define the aws-s3 gem if you want to upload your files to Amazon S3. You do not need to explicitly define the paperclip gem itself, since this is handled bymongoid-paperclip.
Rails.root/Gemfile - Just define the following:
gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem "aws-s3",            :require => "aws/s3"
Next let's assume we have a User model and we want to allow our users to upload an avatar.
Rails.root/app/models/user.rb - include the Mongoid::Paperclip module and invoke the provided class method
class User
  include Mongoid::Document
  include Mongoid::Paperclip

  has_mongoid_attached_file :avatar
end

That's it

That's all you have to do. Users can now upload avatars. Unlike ActiveRecord, Mongoid doesn't use migrations, so we don't need to define the Paperclip columns in a separate file. Invoking the has_mongoid_attached_file method will automatically define the necessary :avatar fields for you in the background.

include  :html => { :multipart => true } for form.

Friday, 24 August 2012

Mongo Restart/connection failure ?






Connection failed during mongo start:


Follow this steps:
  • $   sudo rm /var/lib/mongodb/mongod.lock
  • $   mongod --repair
  • $   sudo start mongodb
  • $   sudo status mongodb
  • $   mongo
                      connection established message will be displayed.  :)

Tuesday, 7 August 2012

What and Why Mongo DB?


                     We built MongoDB from our own experiences building large scale, high availability, robust systems. We didn’t start from scratch, we really tried to figure out what was broken, and tackle that. MongoDB is that if you take MySql, and change the data model from relational to document based, you get a lot of great features: embedded docs for speed, manageability, agile development with schema-less databases, easier horizontal scalability because joins aren't as important. There are lots of things that work great in relational databases: indexes, dynamic queries and updates to name a few, and we haven’t changed much there. For example, the way you design your indexes in MongoDB should be exactly the way you do it in MySql or Oracle, you just have the option of indexing an embedded field.

Why MongoDB?

  • Document-oriented

    • Documents (objects) map nicely to programming language data types
    • Embedded documents and arrays reduce need for joins
    • Dynamically-typed (schemaless) for easy schema evolution
    • No joins and no multi-document transactions for high performance and easy scalability
  • High performance

    • No joins and embedding makes reads and writes fast
    • Indexes including indexing of keys from embedded documents and arrays
    • Optional streaming writes (no acknowledgements)
  • High availability

    • Replicated servers with automatic master failover
  • Easy scalability

    • Automatic sharding (auto-partitioning of data across servers)
    • Reads and writes are distributed over shards
    • No joins or multi-document transactions make distributed queries easy and fast
    • Eventually-consistent reads can be distributed over replicated servers
  • Rich query language

Saturday, 23 June 2012

Mongo db Quieries


Here is the some important frequently used mongodb queries.
After running mongodb server(mongod) and  interactive shell(mongo),  Our’s first command should be
1. help
->Which will show a lot  of useful command that is helpful at any instance working with mongodb.
2.db.help();
->It is database level help command that will show some of data level command.
3. db.help;
-> This command will show database level methods.
All above command is basic mongodb command.
How to create database?
show dbs; to show all the databases...
show collections; to view all the table in db...
1. use databasename;
In my case it is “use mongotest”,  here USE  is command to create database and  mongotest is database name. It is one of the nice feature of mongodb if there will be no database with this particular name, will create new database else use(move) to exiting database.
 Create table!!!!!!!
1. db.tablename.save({“name”:”ankit”,”phone”:”09533667506″});
Fetch data from tables!!!!!!!!!!
1. db.tablename.find();
return data of table.
2.db.tablename.find;
return methods it self.
Create another record!!!!!!!!!
x={“name”:”abhay”,”phone”:”09932329856″}
db.contacts.save(x);
this is another way of creating new record and saving as an object.
Fetch particular record from tables !!!!!!!!!!
db.contacts.find({“name”:”ankit”});
N.T. -> Search option is case sensetive.
find record using id
> db.contacts.find({_id:ObjectId("4ea3aa28eaaf3871b0549dfa")});
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "09533607506" }
Update particular record into tables !!!!!!!!!!
-> there is two way to update table
1. Using save()->this is one of nice feature to mongodb, if there will already row in table it will update with new attribute else will create new record.
2. Using update()->will only update exiting record.
find record using object
> x = db.contacts.findOne({"name":"ankit"});
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "09533607506"
}
Now we have object x with reference of contacts table.
> x
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "09533607506"
}
Now i am going to add new attribute email
> x.email = "ankit@gmail.com"
ankit@gmail.com
Let's see updated row 
> x
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "09533607506",
        "email" : "ankit@gmail.com"
}
Now we fetch details from table
> db.contacts.find();
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "09533607506" }
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"),
 "name" : "abhay", "phone" : "9932326589" }
table is not updated here, we need to update table.
> db.contacts.save(x);
Now try again
> db.contacts.find();
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"), 
"name" : "abhay", "phone" : "9932326589" }
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "09533607506",
 "email" : "ankit@gmail.com" }
Delete particular record from tables !!!!!!!!!!
> db.contacts.remove(x);
let's see row is there or not
> db.contacts.find();
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"),
 "name" : "abhay", "phone" : "9932326589" }
yah, this row is deleted
THAT ALL WAS THE CRUD OPERATION ON MONGODB TABLE.

Tuesday, 5 June 2012

Mongo DB


Issue the following command to install the latest stable version of MongoDB:
sudo apt-get install mongodb-10gen
When this command completes, you have successfully installed MongoDB! Continue for configuration and start-up suggestions.



Starting MongoDB

Upstart users can start the mongod process by issuing the following command:
sudo service mongodb start
All other users can issue the following command to start mongod:
sudo /etc/init.d/mongodb start
You can verify that mongod has started successfully by checking the contents of the log file at /var/log/mongodb/mongodb.log.

Stopping MongoDB

Upstart users can stop the mongod process by issuing the following command:
sudo service mongodb stop
All other users can issue the following command to stop mongod:
sudo /etc/init.d/mongodb stop

Restarting MongoDB

Upstart users can restart the mongod process by issuing the following command:
sudo service mongodb restart
All other users can issue the following command to restart mongod:
sudo /etc/init.d/mongodb restart
type "mongo" in your terminal to see the version