As discussed in the previous post we learned to install and
connect to MongoDB from the command line
We can see there is no document in the techon_col collection
In this post will go ahead with creating a Database, Collection, and Documents and perform different operations in MongoDB
Create Database
Connect to MongoDB on the command line by using the "mongo" command
Creating a mongo database is just like creating a directory structure, in the below example I have
created a database named "techon_db"
Below screenshot will show the steps
We can see "use " command will be able to
create a database and same way we can switch to another database as well
"db" command will show the current db and show
dbs will list all the databases present in an instance with some data in it.
The empty database will not be shown here.
Drop Database
In the similar way, dropping of db is very simple, just invoke db.dropDatabase(); from the database, you
want to drop
> use mydb
switched to db mydb
> db.dropDatabase ();
{ "ok" : 1 }
Create Collection
As mentioned Introduction of MongoDB, the collection is analogous to a table in RDMBS and is created using BSON (binary JSON) coding, here we will
create a simple collection under our recently created db "techon_db"
There is a couple of ways to do it.
1. Using db.createCollection ("collectionname")
I have created a collection with the name
"techon_col" and you can see the same as shown below
>use techon_db;
switched to db techon_db
>
>show collections;
>
>db.createCollection ("techon_col");
{"ok" : 1 }
>show collections;
techon_col
2. By inserting documents in collection
In the below example, we are creating collection techon_col1 and
inserting documents in it. This method performs both steps within a single
command. It will automatically create a collection and insert a document in it
>show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
techon_db 0.000GB
>
>db
techon_db
>show collections;
techon_col
>
>db.techon_col1.insert({name: "John"});
WriteResult({"nInserted" : 1 })
>
>show collections;
techon_col
techon_col1
Dropping Collection
In the below
example I have dropped collection techon_col1.
>show collections
techon_col
techon_col1
>db.techon_col1.drop();
true
>show collections
techon_col
Insert and Remove Documents
Now we
will try to insert data in collection techon_col, first, check if it has any
data in it using find ()
>use techon_db
switched to db
techon_db
>show collections;
techon_col
techon_col1
>db.techon_col1.find();
{"_id" :
ObjectId("5bfa6a9a131a6cd76ed5cd4c"),"name" :
"John" }
>db.techon_col.find();
>
We can see there is no document in the techon_col collection
Below I
have added two documents in the collection with three attributes
Rno,
firstname and
lastname.
"_id" is the default attribute generated by MongoDB,it will
work as primary key
db.techon_col.find();
>
>db.techon_col.insert({"Rno":"10",
"firstname":"Mark",
"lastname":"steve"});
WriteResult({"nInserted" : 1 })
>
>db.techon_col.insert({"Rno":"11",
"firstname":"Dave",
"lastname":"john"});
WriteResult({"nInserted" : 1 })
>
>db.techon_col.find();
{"_id" :
ObjectId("5bfa7162131a6cd76ed5cd4f"),
"Rno" : "10", "firstname" :
"Mark",
"lastname" : "steve" }
{"_id" :
ObjectId("5bfa718a131a6cd76ed5cd50"),
"Rno" : "11", "firstname" :
"Dave",
"lastname" : "john" }
>
Removing Document
In below
example I have removed document with Rno 10
>db.techon_col.remove({"Rno":"10"});
WriteResult({"nRemoved" : 1 })
>db.techon_col.find();
{"_id" :
ObjectId("5bfa718a131a6cd76ed5cd50"),
"Rno" : "11", "firstname" :
"Dave",
"lastname" : "john" }
>
Here is the
summary screenshot of activities we have performed
[Also read - CRUD operations in Mongodb]
In case you want to check basics of MongoDB, Visit Introduction to MongoDB
0 comments:
Post a Comment