MognoDB Tutorial
MognoDB Tutorial - Chapter 1
MongoDB is an open-source document database, and leading
NoSQL database. MongoDB is written in c++.
I am going to tell you that how can you learn MongoDB:
TAKE A LOOK ON THIS TABLE FOR A SIMPLE BRIEF
RDBMS |
MongoDB
|
Database
|
Database
|
Table
|
Collection
|
Tuple/Row
|
Document
|
Column
|
Field
|
Table Join
|
Embedded Documents
|
Primary Key
|
Primary Key (Default key _id provided by mongodb itself)
|
Database Server and Client
|
|
Mysqld/Oracle
|
mongod
|
mysql/sqlplus
|
mongo
|
First step: You need to go in MongoDB folder where you had installed it
Like: C:\wamp\bin\mongodb\mongodb-win32-x86_64-2008plus-2.4.10\bin
Now you need to start mongodb server to double click on mongod.exe if it is already start if
will blink the cmd and close.
Now you need to Double click on mongo.exe to cmd interface to run mongoDB command
It will show something like this:-
Now you need to Double click on mongo.exe to cmd interface to run mongoDB command
It will show something like this:-
MongoDB shell
version: 2.4.10
Connection to: test
>
Connection to: test
>
Here you can write the command for MongoDB
1) First of all you need to create a database in mongodb
1) First of all you need to create a database in mongodb
Run this command: use
yourdb
The command will create a new database, if it doesn't exist otherwise it will return the existing database.
The command will create a new database, if it doesn't exist otherwise it will return the existing database.
After that run this command db press enter then write your DBname name
expample:
use mydb
db
mydb
The db will now show until you will not add at least a collection in database
So to insert the collection into the database you need run this command
db.createCollection("mycollection", options)
expample:
use mydb
db
mydb
The db will now show until you will not add at least a collection in database
So to insert the collection into the database you need run this command
db.createCollection("mycollection", options)
Press enter
Now you can check via this command that is you database and
collection created or not
show dbs
2) To check all database name, you need to enter this command
show dbs
Result:
laravel 0.2.3125GB
Twitter 0.2.3556GB
Testdb 0.2.3245GB
3) Now you need to switch databsae which one you want to use
use dbname
laravel 0.2.3125GB
Twitter 0.2.3556GB
Testdb 0.2.3245GB
3) Now you need to switch databsae which one you want to use
use dbname
Result:
switched to db dbname
switched to db dbname
4) Now if you
want to check the collection of database - run this command
show collections
show collections
Result:
system.indexes
user
comment
link
Post
system.indexes
user
comment
link
Post
5) To delete a
database
first switched a database which you have already created
use dbname
Now write this command to delete the selected database.
Please keep in mind before deleting any database switched the current database
db.dropDatabase()
Example:
>use mydb
switched
to db mydb
>db.dropDatabase()
>{
"dropped" : "mydb", "ok" : 1 }
>
6) To create the collection inside the database
Use this command:
use dbname
db.createCollection("mycollection", options) Options parameter is optional for size and capped value so leave it if you do not know about these parameter
7) To insert data
into a collection
db.twitter.insert({"title":"mongotest"})
8) To drop collection from database
db.COLLECTION_NAME.drop()
Example:
First, check the available collections
into your database mydb
>use
mydb
switched
to db mydb
>show
collections
mycol
mycollection
system.indexes
tutorialspoint
>
Now drop the collection with the name mycollection
>db.mycollection.drop()
true
>
Again check the list of collections
into database
>show
collections
mycol
system.indexes
tutorialspoint
>
9) Get the data from collection
db.COLLECTION_NAME.find()
This will show data as a text on your cmd which is not
formatted to read
If you want show data in formatted manner you can run this
command too
>db.COLLECTION_NAME.find().pretty()
OR in MongoDB
Syntax:
To query documents based on the OR condition, you need to use $or keyword. Basic syntax of OR is shown below:>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Using Like, AND and OR together
Example
Below given example will show the documents that have likes greater than 100 and whose title is either 'MongoDB Overview' or by is 'tutorials point'. Equivalent sql where clause is 'where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'>db.mycol.find("likes": {$gt:10}, $or: [{"by": "tutorials point"}, {"title": "MongoDB Overview"}] }).pretty()
Syntax to update the query: db.collection.update( <query>, <update>,
{ upsert: <boolean>, multi: <boolean>, writeConcern: <document> })
To update the record
db.collection.update({"name":"TOM"}, {$set:{"count":500}})
TO update the record
$set - WE use this keyword to set the new value and replace the last value
if value not found then it create new field to that row db.products.update( { sku: "abc123" },
{ $inc: { quantity: 5 } } )
$inc - We use this keyword to increment to that field which is mention in $inc keyword Like: { $inc: { quantity: 5 } }
so now it will add 5 in quantity likeif quantity was 2 then its value will be now 7.
it works for both + and - the increment like:
Like: { $inc: { quantity: -2 } }
db.mycollection.remove() - To delete the all record from collection or empty the collection
Set Limit in collection:
db.mycol.find().limit(1).skip(1)
Sorting the record:
db.COLLECTION_NAME.find().sort({name:1}) 1 for ascending order-1 for descending order To get the backup of MongoDB database and server mongodump
db.user.ensureIndex({title:1})Db.user.save(varibale):
Example: you can set a variable with its value before and later you can pass thembooks = {"name":"sagar","class":"mongo"}
db.user.save(books)
Indexing: db.user.ensureIndex({name:1})Multi Key indexdb.user.ensureIndex({name:1})
OverView of MongoDB Tutorial:
use yourdb - To create DB or switched to DBdb.createCollection("mycollection", options) - To create collection
show dbs - To display all database show collections - To display all collection from a databasedb.dropDatabase() - To drop the current database
db.createCollection("twitter ", options) - To create the collection inside the db
db.twitter.insert({"title":"mongotest"}) - To insert the data in collection
db.COLLECTION_NAME.drop() - To drop a collection from databaedb.COLLECTION_NAME.find() - To display the data from collection without formatted manner
db.COLLECTION_NAME.find().pretty() - To display the data from collection in formatted manner
Comments
Post a Comment