Skip to main content

Mongodb Basics

Mongodb Basics:


1)mongo
2)show dbs;
3)use sample_mongo_development;
4)show tables || show collections;
> show tables;
articles
system.indexes
5)See the table records
> db.articles.find();
{ "_id" : ObjectId("552d065568657523e5000000"), "name" : "rdfgffdg", "content" : "dfgfdgdfgdfg" }
{ "_id" : ObjectId("552d07b968657523e5010000"), "name" : "xzgfxcgv", "content" : "xcvxvxvcx" }
{ "_id" : ObjectId("552d094768657523e5020000"), "name" : "fgdfg", "content" : "dfgdfg" }
{ "_id" : ObjectId("552f56766865751802000000"), "name" : "drdsf", "content" : "dsfsdf" }
{ "_id" : ObjectId("552f5a1b6865751802010000"), "name" : "dfvxc", "content" : "fdhgdfh" }
{ "_id" : ObjectId("552f5b286865751802020000"), "name" : "dfvxc", "content" : "fdhgdfh", "first_name" : "45566oo" }
{ "_id" : ObjectId("552f61b16865751802030000"), "name" : "kotesh", "content" : "very very good boy" }
6)Where condition

> db.articles.find({name: "kotesh"});
{ "_id" : ObjectId("552f61b16865751802030000"), "name" : "kotesh", "content" : "very very good boy" }

> db.articles.find({content: "very very good boy",name: "kotesh"});
{ "_id" : ObjectId("552f61b16865751802030000"), "name" : "kotesh", "content" : "very very good boy" }
7)db.createCollection("people", { size: 2147483648 }) ---> creating table
8)db.people.insert({"name" : "koti"});  ---> Inserting data to table
9)db.people.find();  ----> Display all records fro table
10)db.people.update({ },{ $set: { join_date: "" } }) -- > add columns
11)db.people.find();
{ "_id" : ObjectId("5530d08b8e0e1c181243c071"), "dob" : "12-feb-1989", "gender" : "female", "join_date" : "", "name" : "sam" }
12)db.users.drop();   --> Drop the table
13)db.college.count;  -> count the number of records
======================
SQL SELECT Statements   MongoDB find() Statements

SELECT *
FROM users

 

db.users.find()

SELECT id,
       user_id,
       status
FROM users

 

db.users.find(
    { },
    { user_id: 1, status: 1 }
)

SELECT user_id, status
FROM users

 

db.users.find(
    { },
    { user_id: 1, status: 1, _id: 0 }
)

SELECT *
FROM users
WHERE status = "A"

 

db.users.find(
    { status: "A" }
)

SELECT user_id, status
FROM users
WHERE status = "A"

 

db.users.find(
    { status: "A" },
    { user_id: 1, status: 1, _id: 0 }
)

SELECT *
FROM users
WHERE status != "A"

 

db.users.find(
    { status: { $ne: "A" } }
)

SELECT *
FROM users
WHERE status = "A"
AND age = 50

 

db.users.find(
    { status: "A",
      age: 50 }
)

SELECT *
FROM users
WHERE status = "A"
OR age = 50

 

db.users.find(
    { $or: [ { status: "A" } ,
             { age: 50 } ] }
)

SELECT *
FROM users
WHERE age > 25

 

db.users.find(
    { age: { $gt: 25 } }
)

SELECT *
FROM users
WHERE age < 25

 

db.users.find(
   { age: { $lt: 25 } }
)

SELECT *
FROM users
WHERE age > 25
AND   age <= 50

 

db.users.find(
   { age: { $gt: 25, $lte: 50 } }
)

SELECT *
FROM users
WHERE user_id like "%bc%"

 

db.users.find( { user_id: /bc/ } )

SELECT *
FROM users
WHERE user_id like "bc%"

 

db.users.find( { user_id: /^bc/ } )

SELECT *
FROM users
WHERE status = "A"
ORDER BY user_id ASC

 

db.users.find( { status: "A" } ).sort( { user_id: 1 } )

SELECT *
FROM users
WHERE status = "A"
ORDER BY user_id DESC

 

db.users.find( { status: "A" } ).sort( { user_id: -1 } )

SELECT COUNT(*)
FROM users

 

db.users.count()

or

db.users.find().count()

SELECT COUNT(user_id)
FROM users

 

db.users.count( { user_id: { $exists: true } } )

or

db.users.find( { user_id: { $exists: true } } ).count()

SELECT COUNT(*)
FROM users
WHERE age > 30

 

db.users.count( { age: { $gt: 30 } } )

or

db.users.find( { age: { $gt: 30 } } ).count()

SELECT DISTINCT(status)
FROM users

 

db.users.distinct( "status" )

SELECT *
FROM users
LIMIT 1

 

db.users.findOne()

or

db.users.find().limit(1)

SELECT *
FROM users
LIMIT 5
SKIP 10

 

db.users.find().limit(5).skip(10)

EXPLAIN SELECT *
FROM users
WHERE status = "A"

 

db.users.find( { status: "A" } ).explain()

For more information, see db.collection.find(), db.collection.distinct(), db.collection.findOne(), $ne $and, $or, $gt, $lt, $exists, $lte, $regex, limit(), skip(), explain(), sort(), and count().
Update Records

The following table presents the various SQL statements related to updating existing records in tables and the corresponding MongoDB statements.
SQL Update Statements   MongoDB update() Statements

UPDATE users
SET status = "C"
WHERE age > 25

 

db.users.update(
   { age: { $gt: 25 } },
   { $set: { status: "C" } },
   { multi: true }
)

UPDATE users
SET age = age + 3
WHERE status = "A"

 

db.users.update(
   { status: "A" } ,
   { $inc: { age: 3 } },
   { multi: true }
)

For more information, see db.collection.update(), $set, $inc, and $gt.
Delete Records

The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements.
SQL Delete Statements   MongoDB remove() Statements

DELETE FROM users
WHERE status = "D"

 

db.users.remove( { status: "D" } )

DELETE FROM users

 

db.users.remove({})

Comments

Popular posts from this blog

Get the Current url in ruby on rails

request.refererrequest.fullpathrequest.fullpath.split("?")[0]request.referer or request.env['HTTP_REFERER'] your controller to get the referer url. 

--------------------
request.original_url
request.url
request.host_with_port
<link rel="canonical" href="<%= request.url %>" />
<%=request.path%>
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
request.env['REQUEST_URI']

 For Rails 3.2 or Rails 4 you should use request.original_url to get the current URL. More detail.

For Rails 3: You want "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

For Rails 2: You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]
end
end