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({})
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
Post a Comment