-
Notifications
You must be signed in to change notification settings - Fork 5
MongoDB (Basics)
Gaurav Chandak edited this page Jul 23, 2016
·
1 revision
#MongoDB (Basics)
MongoDB is a crossplatform, document oriented database that provides high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.
Properties:
- Database is a physical container for collections.
- Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table.
- A document is a set of key-value pairs and have dynamic schema.
Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.
##Commands
use db_name
db
show dbs
db.dropDatabase()
db.createCollection("collection_name", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
show collections
db.collection_name.drop()
db.collection_name.insert([ { name: 'Gaurav', alias: ['gc', 'gc_nit', 'gcnit'] }, { name: 'GauravChandak', alias: ['g.c', 'gc_.nit', 'gc.nit'] } ])
db.collection_name.find()
db.collection_name.find().pretty()
db.collection_name.update({"name" : "Gaurav"}, {$set:{"surname" : "Chandak"}})
db.collection_name.find({"name" : "Gaurav", "surname" : "Chandak"}).pretty()
db.collection_name.find({$or:[{"name" : "Gaurav"}, {"surname" : "Chandak"}]}).pretty()
db.collection_name.save({"_id" : ObjectId("56d676d33cac80f6b4dfe121"), "name" : "gaurav", "surname" : "Chandak"})
db.collection_name.remove({"name": "gaurav"})
db.collection_name.remove({"name": "gaurav"}, 2)
db.collection_name.find({}, {"name":1, "_id":0})
db.collection_name.find({}, {"name":1, "_id":0}).limit(1)
db.collection_name.find({}, {"name":1, "_id":0}).limit(1).skip(1).pretty()
db.collection_name.find({}, {"name":1, "_id":0}).sort({"name":1}).pretty()
db.collection_name.find({}, {"name":1, "_id":0}).sort({"name":-1}).pretty()
db.collection_name.ensureIndex({"name":1})
db.collection_name.aggregate([{$group : {_id : "$surname", sum : {$sum : 1}}}]).pretty()