Mongo DB Quieries

16, September 2012

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? show dbs; to show all the databases... show collections; to view all the table in db...
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”:”1234567890″});
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”:”1234567890″}
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" : "1234567890" }
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" : "1234567890"
}
Now we have object x with reference of contacts table.
> x
{
        "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
        "name" : "ankit",
        "phone" : "1234567890"
}
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" : "1234567890",
        "email" : "ankit@gmail.com"
}
Now we fetch details from table
> db.contacts.find();
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "1234567890" }
{ "_id" : ObjectId("4ea3b067eaaf3871b0549dfb"),
 "name" : "abhay", "phone" : "1234567890" }
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" : "1234567890" }
{ "_id" : ObjectId("4ea3aa28eaaf3871b0549dfa"),
 "name" : "ankit", "phone" : "1234567890",
 "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" : "1234567890" }


yah, this row is deleted THAT ALL WAS THE CRUD OPERATION ON MONGODB TABLE.

Comments