Mongo DB
Mongo DB
MongoDB is a popular document-oriented NoSQL database program that allows for the storage and
retrieval of unstructured and semi-structured data. It is designed to be highly scalable and flexible,
making it a popular choice for many modern web applications and data-driven businesses.
MongoDB is open source and has a thriving developer community that provides a wide range of tools and
resources to support its use. It is used by many large companies, including Cisco, eBay, and Adobe, as
well as many smaller startups and organizations.
Data Storing:
In MongoDB, queries and data are stored in collections. A collection is made up of documents, and each
document represents a record. The fields within a document represent columns, and each document is a
row.
Therefore, the records of all queries in a MongoDB collection would be represented by the rows in that
collection's documents, and the columns would be the individual fields within each document.
Queries in Database:
In MongoDB, queries are used to retrieve data from collections based on certain criteria. The queries are
written in MongoDB's query language, which is based on JSON and provides a flexible and powerful way
to query data.
Create Collection using mongosh
There are 2 ways to create a collection.
Method 1
You can create a collection using the createCollection() database method.
insert Documents
Method 2
You can also create a collection during the insert process.
Insert Documents
There are 2 methods to insert documents into a MongoDB database.
insertOne()
To insert a single document, use the insertOne() method.
This method inserts a single object into the database.
Note: When typing in the shell, after opening an object with curly braces "{" you can press enter to start a
new line in the editor without executing the command. The command will execute when you press enter
after closing the braces.
Find Data
There are 2 methods to find and select data from a MongoDB collection, find() and findOne().
find()
To select data from a collection in MongoDB, we can use the find() method.
This method accepts a query object. If left empty, all documents will be returned.
Update Document
To update an existing document we can use the updateOne() or updateMany() methods.
The first parameter is a query object to define which document or documents should be updated.
The second parameter is an object defining the updated data.
Delete Documents
We can delete documents by using the methods deleteOne() or deleteMany().
These methods accept a query object. The matching documents will be deleted.
deleteOne()
The deleteOne() method will delete the first document that matches the query provided.
deleteMany()
The deleteMany() method will delete all documents that match the query provided.
Logical
The following operators can logically compare multiple queries.
$and: Returns documents where both queries match
$or: Returns documents where either query matches
$nor: Returns documents where both queries fail to match
$not: Returns documents where the query does not match
Evaluation
The following operators assist in evaluating documents.
$regex: Allows the use of regular expressions when evaluating field values
$text: Performs a text search
$where: Uses a JavaScript expression to match documents
These are just a few examples of the types of queries that can be performed in MongoDB. The flexibility
of MongoDB's query language allows for a wide range of complex queries to be performed on collections
of data.