show dbs //Shows all the databases
use facenovel //Creates and switches to a new database called facenovel
db //Tells the current database in use
Basic Structure of a Document
{
first_name : "John",
last_name : "Doe",
memberships : ["mem1","mem2"],
address:{
street : " 4 Main St",
city : "Boston"
},
contacts : [
{name : "Rahul", relationship:"Friend"},
{name : "Tim", relationship:"Son"}
]
}
db.createUser(
{
user: "Rahul",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
db.createCollection('customers')
show collections //Lists all the collections in the current database
Insert a document in a collection
db.customers.insert({firstName:"John",lastName:"Doe"})
db.customers.insert([
{firstName:"Rahul"}, {firstName:"Rahul",lastName:"Bhatia",age:34}
]);
View all the documents in a collection
db.customers.find();
db.customers.find().pretty(); //Displays in a better manner
db.customers.update({firstName:"Rahul"},{firstName:"Rahul",lastName:"Bhatia",gender:"male",age:19});
db.customers.update({firstName:"Rahul"},{$set:{profession:"developer"}});
//Adds the field developer rather than replacing all the existing data
db.customers.update({firstName:"Rahul"},{$inc:{age:5}});
//Increments the age of Rahul by 5 uears