Basic commands on mongo shell
Show all available databases:
show dbs;
Select a database to access, e.g. mydb. This will create mydb if it does not
already exist:
use mydb;
Show all collections in the database (be sure to select one first, see above):
show collections;
Show all functions that can be used with the database:
db.mydb.help();
To check your currently selected database, use the command db
> db mydb
db.dropDatabase() command is used to drop a existing database.
db.dropDatabase()
Chapter 2: CRUD Operation: Create
Insert Methods
MongoDB provides the following methods for inserting documents into a
collection:
db.collection.insertOne() Inserts a single document into a collection.
db.collection.insertMany( db.collection.insertMany() inserts multiple documents into
) a collection.
db.collection.insert() db.collection.insert() inserts a single document or multiple
documents into a collection.
db.people.insert({name: 'Tom', age: 28});
Or
db.people.save({name: 'Tom', age: 28});
The difference with save is that if the passed document contains an _id field,
if a document already exists with that _id it will be updated instead of being
added as new.
Two new methods to insert documents into a collection, in MongoDB 3.2.x:
Use insertOne to insert only one record:
db.people.insertOne({name: 'Tom', age: 28});
Use insertMany to insert multiple records:
db.people.insertMany([{name: 'Tom', age: 28},{name: 'John', age: 25},
{name: 'Kathy', age: 23}])
Note that insert is highlighted as deprecated in every official language driver
since version 3.0. The full distinction being that the shell methods actually
lagged behind the other drivers in implementing the method. The same thing
applies for all other CRUD methods.
Update
db. table1.update({where condition},{$set:{columnname:columnvalue }})
Update the entire object:
db.people.update({name: 'Tom'}, {age: 29, name: 'Tom'})
// New in MongoDB 3.2
db.people.updateOne({name: 'Tom'},{age: 29, name: 'Tom'}) //Will
replace only first matching document.
db.people.updateMany({name: 'Tom'},{age: 29, name: 'Tom'}) //Will
replace all matching documents.
Or just update a single field of a document. In this case age:
db.people.update({name: 'Tom'}, {$set: {age: 29}})
You can also update multiple documents simultaneously by adding a third
parameter. This query will update all documents where the name equals
Tom:
db.people.update({name: 'Tom'}, {$set: {age: 29}}, {multi: true})
// New in MongoDB 3.2
Will update only first matching document.
db.people.updateOne({name: 'Tom'},{$set:{age: 30})
Will update all matching documents.
db.people.updateMany({name: 'Tom'},{$set:{age: 30}})
If a new field is coming for update, that field will be added to the document.
db.people.updateMany({name: 'Tom'},{$set:{age: 30, salary:50000}})
// Document will have `salary` field as well.
If a document is needed to be replaced,
db.collection.replaceOne({name:'Tom'}, {name:'Lakmal',age:25,address:'Sri
Lanka'})
can be used.
Note: Fields you use to identify the object will be saved in the updated
document. Field that are not defined in the update section will be removed
from the document.