Few useful command for MongoDB
MongoDB is an open-source document-oriented database, and leading NoSQL database. MongoDB is a cross-platform that provides high performance, high availability, and easy scalability. MongoDB is built on an architecture of collections and documents. A document is a set of key-value pairs and are the basic unit of data in MongoDB. Collections contain sets of documents and function as the equivalent of relational database tablesx`
Create Database:
# >show dbs
# >use Database_name
# >db.User.insert({"name":"tutorials point"})
For Eg:
>use techoims switched to db techoism >db.movie.insert({"name":"Location"}) WriteResult({ "nInserted" : 1 })
# >db
Drop Database:
# >db.dropDatabase()
For Eg:
>use techoism switched to db techoism >db.dropDatabase() { "dropped" : "techoism", "ok" : 1 } >
Note: If you have not selected any database, then it will delete default ‘test’ database
Create Collection:
# > db.createCollection(name, options)
Note:
Name: Name of the collection to be created
Option: Option is a document and used to specify configuration of collection
For Eg:
>use techoism switched to db techoism >db.movie("mycollection") { "ok" : 1 } >
For Eg:
>db.movie("log", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) { "ok" : 1 }
# >show collections
Drop Collection:
# > db.COLLECTION_NAME.drop()
For Eg:
>use techoism switched to db techosim >show collections log movie name system.indexes >db.movie.drop() true >
Insert Document:
# >db.COLLECTION_NAME.insert(document)
For Eg:
>db.name.insert({User: 'Dennis R', description: 'Linux Administrator', by: 'tutorials point', url: 'http://www.techoism.com'}) WriteResult({ "nInserted" : 1 })
>db.name.insert([ { User: 'Steve J', description: 'Wordpress Developer', by: 'tutorials point', url: 'http://www.techoism.com' }, { User: 'Lina', description: 'Magento Developer', by: 'tutorials point', url: 'http://www.techoism.com' } ])
Query Document:
# >db.COLLECTION_NAME.find()
For Eg:
>db.name.find() { "_id" : ObjectId("56024f64bef780ed433126c0"), "User" : "Dennis R", "description" : "Linux Administrator", "by" : "tutorials point", "url" : "http://www.techoism.com" } { "_id" : ObjectId("56025057bef780ed433126c1"), "User" : "Steve J", "description" : "Wordpress Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" } { "_id" : ObjectId("56025057bef780ed433126c2"), "User" : "Lina", "description" : "Magento Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" }
Note: To display the result in formatted way use following command:
# >db.COLLECTION_NAME.find().pretty()
For Eg:
>db.name.find().pretty() { "_id" : ObjectId("56024f64bef780ed433126c0"), "User" : "Dennis R", "description" : "Linux Administrator", "by" : "tutorials point", "url" : "http://www.techoism.com" } { "_id" : ObjectId("56025057bef780ed433126c1"), "User" : "Steve J", "description" : "Wordpress Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" } { "_id" : ObjectId("56025057bef780ed433126c2"), "User" : "Lina", "description" : "Magento Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" }
# >db.Collection_Name.find({key1:value1, key2:value2}).pretty()
For Eg:
> db.name.find({"User":"Dennis R", "by":"tutorials point"}).pretty() { "_id" : ObjectId("56024f64bef780ed433126c0"), "User" : "Dennis R", "description" : "Linux Administrator", "by" : "tutorials point", "url" : "http://www.techoism.com" }
Note: If you pass multiple keys by separating them by ‘,’ then MongoDB treats it as AND condition, as In above Example.
# >db.Collection_Name.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()
For Eg:
>db.name.find( { $or: [ {"User": "Lina"}, {"description":"Magento Developer"} ] } ).pretty() { "_id" : ObjectId("56025057bef780ed433126c2"), "User" : "Lina", "description" : "Magento Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" }
Reference:
http://www.tutorialspoint.com/mongodb
Enjoy it!