You are on page 1of 20

MongoDb

MongoDB is an open source, document-oriented database. It is designed to be highly scalable and offers
high developer productivity. MongoDB stores data in JSON-like documents which have dynamic schema.

MongoDB Oracle
Stores data in Collections. Stores data in Tables.

Unit of data storage is a Document which Unit of data storage is a Record (table row).
is stored in a Collection.

Collections have dynamic schema i.e. Tables have fixed schema i.e. attributes are pre defined before
documents in collection can have inserting data. Explicit NULL value has to be provided if data is
different attributes. missing for an attribute.
CRUD operations are performed through insert, find, CRUD operations are performed through INSERT,
update and remove operations on Collection object. SELECT, UPDATE and DELETE statements.

PRIMARY KEY uniquely identifies a document in a PRIMARY KEY uniquely identifies a record in a Table.
Collection. PRIMARY KEY field has a predefined name You can choose any name for PRIMARY KEY.
_id.

NOT NULL, UNIQUE, FOREIGN KEY and CHECK NOT NULL, UNIQUE, FOREIGN KEY and CHECK
constraints are not supported. constraints are supported.

Joins and Subquery are not supported. Joins and Subquery are supported.
Insertion
MongoDB insert command is used to add documents (records) into a MongoDB Collection (table).
MongoDB Shell will display information in semi structured key-value (JSON) format.

Insert:

db.emp.insert({_id: 1, ename: "James", designation: "Analyst", salary: 30000});

Multiple record:

db.emp.insert([{_id: 2, ename: "Ethan", designation: "Manager", salary: 90000},

{_id: 3, ename: "Emily", designation: "Analyst", salary: 25000}]);

Certain field:

db.emp.insert({_id: 4, ename: "Jack", salary: 20000});

Insert without_id field: If _id field is not specified then it is automatically generated with a unique value.

db.emp.insert({ ename: "Steve", designation: "Architect", salary: 95000});


Find
MongoDB find command is used to fetch documents (records) from a MongoDB
Collection (table). You can filter for rows and/or columns using filters. Both row and
column filters are optional. If they are not provided then all rows/columns are
fetched.
Use find to retrieve all documents from the collection.
db.emp.find();

Filter documents:
Use first argument to retrieve specific documents based on a given criteria. This is equivalent to
WHERE clause in SQL.
db.emp.find({designation: "Analyst"});

Filter fields:
Use second argument of find to retrieve specific fields. Provide a value of 1 for all the required fields
in the second argument. This is equivalent to SELECT clause in SQL.
db.emp.find({}, {ename: 1, salary: 1});
Suppress_id field:

In order not to display _id field, you have to explicitly suppress it by providing value of 0 for the field in the
second argument.

db.emp.find({}, {ename: 1, salary: 1, _id: 0});


Use $in operator in find to filter for multiple values. This is equivalent to IN clause in SQL.

db.emp.find({salary: {$in: [30000, 90000]}});

Use $or operator to combine multiple conditions. Document is fetched when any one of the conditions are
true. This is equivalent to OR operator in SQL.

db.emp.find({$or: [{designation:"Analyst"}, {ename:"Ethan"}]}, {ename: 1, salary: 1});


Update
MongoDB update command is used to modify documents (records) in a MongoDB Collection (table).
Update existing file: $set operator is used with update statement to update a document.
db.emp.update({_id: 1}, {$set: {salary: 35000}});
Adding new fields: Use $set operator to also provide fields which do not already exist. They will be
added to the document.

db.emp.update({_id: 2}, {$set: {dept: "HR"}});


Update Multiple column: Use comma separated fields in $set to update multiple columns.

db.emp.update({_id: 4}, {$set: {designation: "Analyst", dept: "ETA"}});


Update all documents: Update command works only on the first document matching the given
criteria. We need to specify the option multi as true to update all documents

db.emp.update({}, {$set: {salary: 10000}}, {multi: true});


Replacing documents: Not using $set replaces the complete document with the new one. Any field
not provided in the new document becomes null even though it was existing in the database before the
update.

db.emp.update({_id: 3}, {ename: "Mark", designation: "Manager"});


Remove
MongoDB remove command is used to delete documents (records) from a MongoDB Collection (table).
Use remove operation to delete documents from a collection.

db.emp.remove({_id: 4});
Multiple documents may be deleted by specifying a criteria not based on _id.

db.emp.remove({salary: {$lt: 30000}});


All documents in collection are deleted if criteria is not supplied.

db.emp.remove({});

You might also like