Connect to MongoDB with MongoShell
- mongo -u username database -p
List databases
Create or choose a database
You can just give the command
If the database doesn't exits, Mongo will create it
List collections
Create collection
Below command will create a collection named testcollection to the active database
- db.createCollection("testcollection")
Insert data to collection
- db.testcollection.insert({fn:'Jim', ln:'Morrison'})
Return data from collection
- db.testcollection.find() -> returns all data
- db.testcollection.find().pretty() -> returns all data, but in more readable format
- db.testcollection.find({fn:"Jim"})
- db.testcollection.find({fn:/^T/}) -> starts with T
- db.testcollection.find({fn:/a/}) -> has a
Count documents
- db.testcollection.count()
Update data
- db.testcollection.update({ln:"Morrison"}, {$set: {fn:"Jack"}})
Removing data from collection
- db.testcollection.remove( {fn: 'Jim'})
- db.testcollection.remove() ->deletes all
Remove Collection