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

Create dynamic sitemap on ruby on rails

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site. It’s basically a XML file describing all URLs in your page: The following example shows a Sitemap that contains just one URL and uses all optional tags. The optional tags are in italics. <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">    <url>       <loc>http://www.example.com/</loc>       <lastmod>2005-01-01</lastmod>       <changefreq>monthly</changefreq>     ...

Omniauth Linked in Ruby On Rails

def get_linkedin_user_data      omniauth = request.env["omniauth.auth"]      dat=omniauth.extra.raw_info      linked_app_key = "xxxxxxx"      linkedin_secret_key = "yyyyyyy"      client = LinkedIn::Client.new(linked_app_key,linkedin_secret_key)      client.authorize_from_access(omniauth['credentials']['token'],omniauth['credentials']['secret'])      connections=client.connections(:fields => ["id", "first-name", "last-name","picture-url"])      uid=omniauth['uid']      token=omniauth["credentials"]["token"]      secret=omniauth["credentials"]["secret"]   #linked user data     omniauth = request.env["omniauth.auth"]      data             = omniauth.info      user_name...

Install Rvm on ubuntu

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.0.0-p645 rvm use 2.0.0-p645 --default ruby -v rvm gemset create rails3.2.8 rvm gemset use rails3.2.8 gem install rails -v 3.2.8