#MongoDB Basic Commands
show dbs //Shows all the databases
use facenovel //Creates ans 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"}
]
}
###Create a user
db.createUser(
{
user: "Rahul",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
###Create a Collection
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});