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

Error malloc(): memory corruption nginx with passenger?

Error malloc(): memory corruption nginx with passenger Passenger issue resolving steps :  sudo gem uninstall passenger(uninstall all passenger) sudo gem install passenger sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx --extra-configure-flags=none Update nginx config file with new passenger version and restart the nginx

Lazy loading in rails – Rails Feature

 Lazy loading in rails – Rails Feature ? Lazy loading in rails is the amazing feature provided with rails. In console you might have tried to examine how lazy loading in rails actually works. In this tutorial, we will learn about this Rails - Lazy loading feature with examples. What exactly is Lazy Loading? As the name suggests the data is loaded in lazy manner (Really!) i.e. Your database is queried only when data from the database is required for some kind of manipulation in code. You will get more of this after you read how-to of lazy loading below. How lazy loading works: Whenever you try to get some data from database, For example, users is the database table that you have. And you are querying database to get users having age less than 20. Then, you will write code like, result = User.where("age < 20") when above statement is executed, your database is not queries yet(because the resultant data is not required yet). When you execute following code, records = resu...

Rails Migration Difference between Text and String

Rails Migration Difference between Text and String ? While working with Rails Migration Difference between Text and String is important to be known to every developer. Columns and their data types are finalized while deciding Table structure. This tutorial will help understand difference between String and Text column type and illustrate how to write Rails Migration implementing the same. You might want to read about database.yml files for specifying database configuration for Rails Application. 1. Concepts When String or Text data type is required?     Whenever you require your column to store information which is lengthy in size (Many characters), you need to consider String or Text data type for the column.     Both of them let you store Many(How Many - will see later) characters Difference between String and Text Considering MySQL database Feature     String     Text Length     1 to 255     ...