Mongo DB
Mongo DB
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:
Multiple record:
Certain field:
Insert without_id field: If _id field is not specified then it is automatically generated with a unique value.
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.
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.remove({_id: 4});
Multiple documents may be deleted by specifying a criteria not based on _id.
db.emp.remove({});