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

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'

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

PayUMoney Integration in Ruby on Rails

PayUMoney Integration in Ruby on Rails


Generating Cheksum in Payu
   Hash or Checksum
= sha512(key|txnid|amount|productinfo|firstname|email|u
df1|udf2|udf3|udf4|udf5||||||salt)



Controller Code Will be
def payumoney
  @key = "gtKFFx"
  @salt =  "eCwWELxi"
  val = "#{key|txnid|amount|productinfo|firstname|email|u
df1|udf2|udf3|udf4|udf5||||||#{@salt}"
 @checksum = Digest::SHA512.hexdigest("#{val}")
end

<form action="https://test.payu.in/_payment" method='post' id='sub'  >
    <input type="hidden" name="key" value="<%=@key%>">
    <input type="hidden" name="hash" value="<%=@checksum%>">
    <input type="hidden" name="txnid" value="TXN1234">
    <input type="hidden" name="amount" value="200">
    <input type="hidden" name="firstname" value=&quo…