• About
  • Privacy Policy
  • Contact us
  • Submit a story
Linux-News Linux news from the Blogosphere
  • Article
  • Howto
  • News
  • Opinion
  • Review
  • RSS Feed
  • Twitter
  • Facebook

MongoDB Basics and Usage With Ruby on Rails

By
News
– August 5, 2012Posted in: Article, Howto, Submitted

By Abhishek Kr Singh

MongoDB is a collection-oriented, schema-free and document based database. It is scalable, high-performance, open source and written in C++. A key feature from a developer’s perspective would be its powerful query language.

The official site has pretty good documentation, so we will just summarize the key concepts here.




Terminology:

Document – is a group of key-value pairs (similar to a hash in ruby), where keys are strings and values are any of a rich set of supported data types. You can think of it as a row in a relational
database, with keys being the column names. Document data is stored in BSON (“Binary Serialized document Notation”) format.

Example:
status = { posted_at: Date(“2012-06-19T02:10:11.3Z”),
author: “Mohammad”,
title: “Learning MongoDB basics”,
text: “This can be a very long description… “,
tags: [ “MongoDB”, “Rails” ],
likes: 20 }

BSON – is a binary-encoded serialization of JSON-like documents. BSON is designed to be lightweight, traversable, and efficient. BSON, like JSON, supports embedding of objects and arrays
within other objects and arrays.

Data Types – Mongo supports all basic JSON data types like string, integer, boolean, double, null, array, and object, as well as special data types like date, object_id, binary data, regular expression,
and code.

Collection – is a group of documents, usually having a similar structure. You can think of it as a table in a relational database.

Indexing – is similar to how it works in relational databases. Every document gets a default index on the “_id” attribute, which also acts as a primary key.

Embedding – is the nesting of objects and arrays inside a document like pre-joined data or like a ‘view’ in relational database. So, for example all the comments on a photo can be embedded
within the photo document itself instead of storing them in a different collection.

Example:

status = { posted_at: Date(“2012-06-19T02:10:11.3Z”),
author: “Mohammad”,
title: “Learning MongoDB basics”,
text: “This can be a very long description… “,
tags: [ “MongoDB”, “Rails” ],
likes: 20,
comments: [

{from “Chirag”,
commented_at: Date(“2012-06-19T02:50:11.3Z”),
comment “Yes, it is pretty cool!”}
]}

Links – are references between documents, like foreign keys.

Joins – are not supported in MongoDB. However, some data can be de-normalized and embedded within the parent document (like in the photo example above) to remove the need for joins.

Restrictions – Document element name cannot begin with “$” or contain a “” “_id” is reserved element name (primary key) and cannot be reused for any other purpose.

Syntax:

It would be good to try this out in a mongo console as we go.

Console
~$ mongo
> help // top level help
> db.help() // help on db-specific methods
> db.mycollection.help() // help on collection methods

Query
db.createCollection(“users”) // Create a new collection
db.users.find({}, {name:1,email:1}) // Get name and email of all users
db.users.find({age:33}, {name:1,email:1}) // Get name and email of all users older than 33 years
db.users.findOne({name: ‘mohammad’}) // Find first document with a given name

Insert
db.users.insert({name: ‘chirag’)
doc = {name: ‘mohammad’, email: ‘mohammad@rails.com’}
db.users.save(doc)

Update
db.users.update({name:’mohammad’}, {$set:{name:’Mohammad’}})

Delete & Drop
db.users.remove({name:’mohammad’})
db.users.drop()

For better understanding, you can go through SQL to Mongo reference here.

ORM for Rails:
There are several ActiveRecord replacements for MongoDB, but we have found MongoMapper to be the closest replacement. There is already a great Railscast on how to use MongoMapper with
Rails, so we won’t cover that in this article.


Get more information on: Ruby on Rails
For more information visit: Web Application Developer

Article Source: http://EzineArticles.com/?expert=Abhishek_Kr_Singh

Article Source: http://EzineArticles.com/7145839

Tags: database indexing, mongo, MongoDB, relational database, Ruby, ruby on rails, serialization

About News

No Comments

Start the ball rolling by posting a comment on this article!

Leave a Reply Cancel reply

Your email address will not be published.

  • Suggested Sites
    http://linuxaria.com everything about Linux http://linuxaria.com everything about Linux
  • Curated topic
  • Recent Posts
    • Essential Guide to Oil Filter Cross Reference Charts PDF
    • Master PDF Editing with the Ultimate Guide to Google Docs PDF Editor
    • Your Ultimate Guide to the “Calendario Mundial Qatar 2023 PDF”
    • Discover the Joy of Mother’s Day: Free Printable Coloring Cards PDF
    • Reduce PDF Size: Ultimate Guide to Optimize and Shrink Your PDFs
    • How to Shrink PDF File Size: Tips and Techniques for Optimal Compression
    • How to Make a PDF Fillable: A Comprehensive Guide
    • How to Effortlessly Convert PDF to JPG: A Comprehensive Guide
    • Unlock PDF Mastery: How to Open PDFs in Adobe for Optimal Handling
    • US Blank Map PDF: A Comprehensive Guide for Customization and Optimization
  • Ranking

About Arras WordPress Theme

All site content, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.