Mongodb queries


Here is the some important frequently used mongodb queries.

After running mongodb server(mongod) and  interactive shell(mongo),  Our’s first command should be

1. help

->Which will show a lot  of useful command that is helpful at any instance working with mongodb.

2.db.help();

->It is database level help command that will show some of data level command.

3. db.help;

-> This command will show database level methods.

All above command is basic mongodb command.

How to create database?

1. use databasename;

In my case it is “use mongotest”,  here USE  is command to create database and  mongotest is database name. It is one of the nice feature of mongodb if there will be no database with this particular name, will create new database else use(move) to exiting database.

 Create table!!!!!!!

1. db.tablename.save({“name”:”ankit”,”phone”:”09533667506″});

Fetch data from tables!!!!!!!!!!

1. db.tablename.find();

return data of table.

2.db.tablename.find;

return methods it self.

Create another record!!!!!!!!!

x={“name”:”abhay”,”phone”:”09932329856″}

db.contacts.save(x);

this is another way of creating new record and saving as an object.

Fetch particular record from tables !!!!!!!!!!

db.contacts.find({“name”:”ankit”});

N.T. -> Search option is case sensetive.

find record using id

> db.contacts.find({_id:ObjectId("4ea3aa28eaaf3871b0549dfa")});
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "09533607506" }

Update particular record into tables !!!!!!!!!!

-> there is two way to update table

1. Using save()->this is one of nice feature to mongodb, if there will already row in table it will update with new attribute else will create new record.

2. Using update()->will only update exiting record.

find record using object

> x = db.contacts.findOne({"name":"ankit"});
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "09533607506"
}
Now we have object x with reference of contacts table.
> x
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "09533607506"
}

Now i am going to add new attribute email

> x.email = "ankit@gmail.com"
ankit@gmail.com
Let's see updated row 
> x
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "09533607506",
        "email" : "ankit@gmail.com"
}

Now we fetch details from table

> db.contacts.find();
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "09533607506" }
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"),
 "name" : "abhay", "phone" : "9932326589" }

table is not updated here, we need to update table.

> db.contacts.save(x);
Now try again
> db.contacts.find();
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"), 
"name" : "abhay", "phone" : "9932326589" }
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "09533607506",
 "email" : "ankit@gmail.com" }

Delete particular record from tables !!!!!!!!!!

> db.contacts.remove(x);
let's see row is there or not
> db.contacts.find();
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"),
 "name" : "abhay", "phone" : "9932326589" }
yah, this row is deleted
THAT ALL WAS THE CRUD OPERATION ON MONGODB TABLE.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s