Mongodb
Mongodb
What is MongoDB
FirstName = "John",
Address = "Detroit",
Spouse = [{Name: "Angela"}]
. FirstName ="John",
Address = "Wick"
Features of MongoDB
1. Support ad hoc queries
2. Indexing
3. Replication
4. Duplication of data
5. Load balancing
6. Supports map reduce and aggregation tools.
7. Uses JavaScript instead of Procedures.
8. It is a schema-less database written in C++.
9. Provides high performance.
10. Stores files of any size easily without complicating your
stack.
11. Easy to administer in the case of failures.
INDEXING
• Indexes support efficient execution of queries in MongoDB. Without
indexes, MongoDB must scan every document in a collection to return
query results. If an appropriate index exists for a query, MongoDB
uses the index to limit the number of documents it must scan.
• Although indexes improve query performance, adding an index has
negative performance impact for write operations. For collections with
a high write-to-read ratio, indexes are expensive because each insert
must also update any indexes.
• _id is default index in MongoDB
Index Names
• You cannot rename an index once created. Instead, you must drop
and recreate the index with a new name.
Create an Index
• This example creates a single key descending index on the name field:
• db.collection.getIndexes()
• Output:
• [
• { v: 2, key: { _id: 1 }, name: '_id_1' },
• { v: 2, key: { name: -1 }, name: 'name_-1' }
• ]
Specify an Index Name
• When you create an index, you can give the index a custom name. Giving
your index a name helps distinguish different indexes on your collection. For
example, you can more easily identify the indexes used by a query in the
query plan's explain results if your indexes have distinct names.
• To specify the index name, include the name option when you create the
index:
• db.<collection>.createIndex(
• { <field>: <value> },
• { name: "<indexName>" }
•)
Drop an Index
• You can remove a specific index from a collection. You may need to drop an index if
you see a negative performance impact, want to replace it with a new index, or no
longer need the index.
• db.collection.dropIndex()
Drops a specific index from the collection.
• db.collection.dropIndexes()
Drops all removable indexes from the collection or an array of indexes, if specified.
NoSQL Database
1. Apache Cassandra
2. Redis
3. OrientDB
4. DynamoDB
5. CouchDB
6. ArangoDB
7. RethinkDB
Redis
• Redis stores data in RAM, so you can access data directly from
memory. While this provides low-latency responses, it also
limits the volume of data you can store. Redis saves the dataset
to disk through snapshotting and append-only file (AOF)
logging, which provides data durability.
• Redis stores data as key-value pairs, where each data entry
has a unique key. It supports various data types like sorted sets,
hashes, sets, lists, and strings. Keys can be any length
Key differences: Redis vs. MongoDB
• Scaling
Horizontal scaling enables MongoDB to handle large volumes of data efficiently. It uses
sharding to distribute data across multiple regions and nodes. Redis doesn’t offer the same
degree of scalability as MongoDB. Redis only uses a single shard by default for primary
operations.
• Availability
Both MongoDB and Redis support availability through replication. However, MongoDB
supports a higher degree of availability by using replica sets. MongoDB can create up to 50
copies of your data. In contrast, Redis doesn’t provide automatic failover by default.
• Integrity
MongoDB supports multi-document atomic, consistent, isolated, and durable (ACID)
transactions. Conversely, Redis does not provide built-in ACID support.
• Query language
MongoDB provides a high level of flexibility in its querying, even performing complex
spatial computations and data analysis functions. In contrast, Redis is optimized for fast
key-value access operations rather than complex querying and searching capabilities.
CouchDB
• Apache CouchDB developed by Apache Software Foundation and
initially released in 2005. CouchDB is written in Erlang. It is an open-
source database that uses different formats and protocols to store,
transfer, and process its data. Apache CouchDB uses JSON to store
data, JavaScript as its query language using MapReduce. Documents
are the primary unit of data in CouchDB and they also include
metadata. Document fields are uniquely named and contain values of
varying types and there is no set limit to text size or element count.
CouchDB MongoDB
JSON format. BSON format.
The database contains documents. The database contains collections.
It favors availability. It favors consistency.
It is written in Erlang. It is written in C++.
It is eventually consistent. It is strongly consistent.
MongoDB is faster than CouchDB. MongoDB provides faster read speeds.
It follows Map/Reduce creating a collection and
It follows the Map/Reduce query method.
object-based query language.
It uses HTTP/REST-based interface. It uses a TCP/IP based interface.
CouchDB provides support for Mobile
Devices.
MongoDB provides no mobile support.
It can run on Apple iOS and Android
devices.
CouchDB offers master-master MongoDB offers master-slave
and master-slave replication. replication.
CouchDB is not suitable for a
rapidly growing database where MongoDB is an apt choice for a
the structure is not clearly defined rapidly growing database.
from the beginning.
CouchDB uses map-reduce
MongoDB is easier to learn as it is
functions and it will difficult for
closest in syntax to SQL.
users with a traditional SQL
learning experience.
It follows MVCC (Multi Version
It follows Update-in-place.
Concurrency Control).
History behind the creation of NoSQL
Databases
• In the early 1970, Flat File Systems are used. Data were stored in flat files and
the biggest problems with flat files are each company implement their own
flat files and there are no standards. It is very difficult to store data in the files,
retrieve data from files because there is no standard way to store data.
• Then the relational database was created by E.F. Codd and these databases
answered the question of having no standard way to store data. But later
relational database also get a problem that it could not handle big data, due to
this problem there was a need of database which can handle every types of
problems then NoSQL database was developed.
Advantages of NoSQL
Date This datatype stores the current date or time in UNIX time format. It
makes you possible to specify your own date time by creating object
of date and pass the value of date, month, year into it.
How to download MongoDB
local 0.078GB
Here, your created database “Rajandb" is not present in the list, insert at least
one document into it to display database:
>db.movie.insert({"name":“RAJAN SALUJA"})
WriteResult({ "nInserted": 1})
>show dbs
var Allcourses =
[ {
Course: "Web Designing",
{
details: { Duration: "3 months", Trainer: "Rashmi Desai" },
Course: "Java",
details: { Duration: "6 months", Trainer: "Sonoo Jaiswal" },
Batch: [ { size: "Small", qty: 5 }, { size: "Large", qty: 10 } ]
Batch: [ { size: "Medium", qty: 25 } ], ,
category: "Programming Language" category: "Programming Language"
}, }
{ ];
Course: ".Net", Inserts the documents
details: { Duration: "6 months", Trainer: "Prashant Verma" }, Pass this Allcourses array to the db.collection.insert() method to
perform a bulk insert.
Batch: [ { size: "Small", qty: 5 }, { size: "Medium", qty: 10 }, > db.Collection1.insert( Allcourses );
],
category: "Programming Language"
},
MongoDB update documents
In MongoDB, update() method is used to update or modify the existing documents of a collection.
Syntax:
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Example
Consider an example which has a collection name collection1. Insert the following documents in collection:
db.Rajandb.insert(
{
course: “Node",
details: {
duration: "6 months",
Trainer: “Rajan"
},
Batch: [ { size: "Small", qty: 15 }, { size: "Medium", qty: 25 } ],
category: "Programming language"
}
)
1. >db.collection1.update({'course':'java'},{$set:{'course':'android'}})
• Check the updated document in the collection:
{
_id : '123'
friends: [
{name: 'allen', emails: [{email: '11111', using: 'true'}]}
]
}
modify user's friends' emails ' email, whose _id is '123
try {
// Connect to the MongoDB cluster
await client.connect();
console.log("database created")
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);
Creating a Collection
• const {MongoClient} = require('mongodb');
• async function main(){
• const url ="mongodb://localhost:27017"
• const client = new MongoClient(url);
• var dbo = client.db("ram");
• console.log(dbo.databaseName);
• await client.connect();
• dbo.createCollection("customers1");
• console.log("Collection created!");
• }
• main().catch(console.error);
Insert Into Collection
To insert a record, or document as it is called in MongoDB, into a collection, we use the
insertOne() method.
A document in MongoDB is the same as a record in MySQL
The first parameter of the insertOne() method is an object containing the name(s) and
value(s) of each field in the document you want to insert.
• Find One
To select data from a collection in MongoDB, we can use the
findOne() method.
The findOne() method returns the first occurrence in the selection.
The first parameter of the findOne() method is a query object. In this
example we use an empty query object, which selects all documents in
a collection (but returns only the first document).
Example
• const {MongoClient} = require('mongodb');
• async function main(){
• const url ="mongodb://localhost:27017"
• const client = new MongoClient(url);
• var dbo = client.db("ram");
• console.log(dbo.databaseName);
• await client.connect();
• const custom = dbo.collection("customers1");
• const query = { name: "Amy"};
• const cust1 = await custom.findOne(query);
• console.log(cust1);
• }
• main().catch(console.dir);
Node.js MongoDB Query
Filter the Result
When finding documents in a collection, you can filter the result by using a query object.
The first argument of the find() method is a query object, and is used to limit the search.
const {MongoClient} = require('mongodb');
async function main(){
const url ="mongodb://localhost:27017"
const client = new MongoClient(url);
var dbo = client.db("ram");
console.log(dbo.databaseName);
await client.connect();
const custom = dbo.collection("customers1");
const query={name:"Company Inc",address:"Highway 37"};
const cust1 = await custom.findOne(query);
console.log(cust1);
}
main().catch(console.dir);
Filter With Regular Expression
• You can write regular expressions to find exactly what you are searching for.
• Regular expressions can only be used to query strings.
• To find only the documents where the "address" field starts with the letter “H", use the
regular expression /^H/:
const {MongoClient} = require('mongodb');
async function main(){
const url ="mongodb://localhost:27017"
const client = new MongoClient(url);
var dbo = client.db("ram");
console.log(dbo. ollection("customers1");
const query={addresdatabaseName);
await client.connect();
const custom = dbo.cs: /^H/ };
const cust1 = await custom.find(query).toArray();
console.log(cust1);
}
main().catch(console.dir);
MongoDB Covered Query
• The MongoDB covered query is one which uses an index and does not
have to examine any documents. An index will cover a query if it
satisfies the following conditions:
• All fields in a query are part of an index.
• All fields returned in the results are of the same index.
• Consider a document in examples collection.
•{
• "_id": ObjectId("53402597d852426020000002"),
• "contact": "1234567809",
• "dob": "01-01-1991",
• "gender": "M",
• "name": "ABC",
• "user_name": "abcuser"
•}
• >db.examples.createIndex({gender:1,user_name:1})
• >db.examples.find({gender:"M"},{user_name:1,_id:0})
• From the above examples, we can say that MongoDB will not look into
database documents but it will fetch the required data from indexed data
Node.js MongoDB Sort
• Sort the Result
use the sort() method to sort the result in ascending or descending order.
The sort() method takes one parameter, an object defining the sorting order.
const {MongoClient} = require('mongodb');
async function main(){
const url ="mongodb://localhost:27017"
const client = new MongoClient(url);
var dbo = client.db("ram");
console.log(dbo.databaseName);
await client.connect();
const custom = dbo.collection("customers1");
var mysort = { name: 1 };
const cust1 = await custom.find().sort(mysort).toArray();
console.log(cust1);
}
main().catch(console.dir);
{ name: -1 } // descending
Node.js MongoDB Delete
• Delete Document
To delete a record, or document as it is called in MongoDB, we use the deleteOne()
method.The first parameter of the deleteOne() method is a query object defining which
document to delete.
Note: If the query finds more than one document, only the first occurrence is deleted.
const {MongoClient} = require('mongodb');
async function main(){
const url ="mongodb://localhost:27017"
const client = new MongoClient(url);
var dbo = client.db("ram");
console.log(dbo.databaseName);
await client.connect();
const custom = dbo.collection("customers1");
const query={name:"Company Inc",address:"Highway 37"};
const cust1 = await custom.deleteOne(query);
console.log("One doc deleted");
}
main().catch(console.dir);
Delete Many
• To delete more than one document, use the deleteMany() method.
The first parameter of the deleteMany() method is a query object defining which
documents to delete.
Example
Delete all documents were the address starts with the letter "O":
• db.dropAllUsers();
• Update Document
Node.js MongoDB Update
You can update a record, or document as it is called in MongoDB, by using the
updateOne() method. The first parameter of the updateOne() method is a query object
defining which document to update.
• Note: If the query finds more than one record, only the first occurrence is updated. The
second parameter is an object defining the new values of the document.
const {MongoClient} = require('mongodb');
async function main(){
const url ="mongodb://localhost:27017"
const client = new MongoClient(url);
var dbo = client.db("ram");
console.log(dbo.databaseName);
await client.connect();
const custom = dbo.collection("customers2");
var myquery = { address: "Canyon 123" };
var newvalues = { $set: {address: "Kharar" } };
const cust1 = await custom.updateOne(myquery, newvalues);
console.log(cust1);
}
main().catch(console.dir);
Update Many Documents
To update all documents that meets the criteria of the query, use the updateMany() method.
Update all documents where the name starts with the letter "S":
const {MongoClient} = require('mongodb');
async function main(){
const url ="mongodb://localhost:27017"
const client = new MongoClient(url);
var dbo = client.db("ram");
console.log(dbo.databaseName);
await client.connect();
const custom = dbo.collection("customers2");
var myquery = { address: /^S/ };
var newvalues = { $set: {name: "Rajan" } };
const cust1 = await custom.updateMany(myquery, newvalues);
console.log(cust1);
}
Node.js MongoDB Limit
To limit the result in MongoDB, we use the limit() method. The limit() method
takes one parameter, a number defining how many documents to return. Consider
you have a "customers" collection and you want to limit the result upto 5 docs:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
Level Description
0 The profiler is off and does not collect any data. This is the
default profiler level.
1 The profiler collects data for operations that take longer
than the value.
• Database: MCA
• Collection: students
db.studentsMarks.distinct("name")
Concurrency
MongoDB allows multiple clients to read and write the same data. To
ensure consistency, MongoDB uses locking and concurrency control to
prevent clients from modifying the same data simultaneously. Writes to a
single document occur either in full or not at all, and clients always see
consistent data.
Concurrency
• MongoDB uses multi-granularity locking that allows operations to
lock at the global, database or collection level, and allows for
individual storage engines to implement their own concurrency
control below the collection.
• MongoDB uses reader-writer locks that allow concurrent readers
shared access to a resource, such as a database or collection.
• In addition to a shared (S) locking mode for reads and an exclusive
(X) locking mode for write operations, intent shared (IS) and intent
exclusive (IX) modes indicate an intent to read or write a resource
using a finer granularity lock. When locking at a certain granularity,
all higher levels are locked using an intent lock.
What is CRUD in MongoDB?
• CRUD operations describe the conventions of a user-interface that let users
view, search, and modify parts of the database.
• MongoDB documents are modified by connecting to a server, querying the
proper documents, and then changing the setting properties before sending the
data back to the database to be updated. CRUD is data-oriented, and it’s
standardized according to HTTP action verbs.
• When it comes to the individual CRUD operations:
• The Create operation is used to insert new documents in the MongoDB
database.
• The Read operation is used to query a document in the database.
• The Update operation is used to modify existing documents in the database.
• The Delete operation is used to remove documents in the database.
Primary-key, Foreign-key Relationship in
Mongo DB
By default, MongoDB doesn't support primary key-foreign key
relationships. Every document in MongoDB contains an _id key field
that uniquely identifies the document. However, this concept can be
implemented by embedding one copy inside another.
Data Models in MongoDB
• Embedded data model: in this model, we store related pieces of
information in a single database record. As a result, applications will
need to issue fewer database calls to retrieve or update data. This
embedded document model is also known as a de-normalized data
model.
Accessing embedded/nested documents
• Syntax:
• "field.nestedField": value
Example
• db.Courses.find({name: {first: "Rohit",
• middle: "Kumar",
• last: "Singh"}}).pretty()
The name field has three nested fields first, middle and last.
Normalized Data Models
Namespace in MongoDB
• The canonical name for a collection or index in MongoDB. The
namespace is a combination of the database name and the name of the
collection or index, like so: [database-name].[collection-or-index-
name]. All documents belong to a namespace.
• MongoDB stores BSON (Binary Interchange and Structure Object
Notation) objects in the collection. The concatenation of the collection
name and database name is called a namespace.
Sharding
There are some best practices you should follow when using the MongoDB
backup and restore services for your MongoDB clusters.
1.MongoDB uses both regular JSON and Binary JSON (BSON) file formats.
It’s better to use BSON when backing up and restoring. While JSON is easy to
work with, it doesn’t support all of the data types that BSON supports, and it
may lead to the loss of fidelity.
2.You don’t need to explicitly create a MongoDB database, as it will be
automatically created when you specify a database to import from. Similarly, a
structure for a collection will be created whenever the first document is
inserted into the database.
3. When creating a new cluster, you have the option to turn on cloud
backup. While you can also enable cloud backups when modifying
an existing cluster, you should turn this feature on by default, as it
will prevent data loss.
4.If a snapshot fails, Atlas will automatically attempt to create
another snapshot. While you can use a fallback snapshot to restore a
cluster, it should only be done when absolutely necessary. Fallback
snapshots are created using a different process, and they may have
inconsistent data.
5.Use secondary servers for backups as this helps avoid degrading
the performance of the primary node.
6. Time the backup of data sets around periods of low
bandwidth/traffic. Backups can take a long time, especially if the
data sets are quite large.
7. Use a replica set connection string when using unsupervised
scripts. A standalone connection string will fail if the MongoDB
host proves unavailable.
Backup Types
• Step 1: You can visit the link Install mongoose to install the mongoose
module. You can install this package by using this command.
• var book1 = new Book({ name: 'Introduction to Mongoose', price: 10, quantity: 25 });
• book1 is a Document of model Book.
Mongoose – Insert Document to MongoDB
// define Schema
var BookSchema = mongoose.Schema({
name: String,
price: Number,
quantity: Number
});
• // compile schema to model
• var Book = mongoose.model('Book', BookSchema,
'bookstore');
// a document instance
• var book1 = new Book({ name: 'Introduction to Mongoose',
price: 10, quantity: 25 });
• // save model to database
• book1.save().then(function () {console.log("document
inserted"); })
• .catch(function (error) {
• console.log(error);
• });
• });
Node.js Mongoose – Insert Multiple Documents
to MongoDB
• To insert Multiple Documents to MongoDB using Mongoose, use
Model.collection.insert(docs_array, options, callback_function);
method. Callback function has error and inserted_documents as
arguments.
• promise
• .then(function () {
• console.log("Promise resolved successfully");
• })
• .catch(function () {
• console.log("Promise is rejected");
• });
Async/Await:
• Async/Await is used to work with promises in asynchronous functions. It is
basically syntactic sugar for promises. It is just a wrapper to restyle code and
make promises easier to read and use. It makes asynchronous code look more
like synchronous/procedural code, which is easier to understand.
• await can only be used in async functions. It is used for calling an async
function and waits for it to resolve or reject. await blocks the execution of the
code within the async function in which it is located.
• Error Handling in Async/Await: For a successfully resolved promise, we
use try and for rejected promise, we use catch. To run a code after the
promise has been handled using try or catch, we can .finally() method. The
code inside .finally() method runs once regardless of the state of the promise.
const helperPromise = function () {
• const promise = new Promise(function (resolve, reject) {
• const x = “rajan";
• const y = “rajan";
• if (x === y) {
• resolve("Strings are same");
• } else {
• reject("Strings are not same");
• }
• });
• return promise;
• };
• async function demoPromise() {
• try {
• let message = await helperPromise();
• console.log(message);
• } catch (error) {
• console.log("Error: " + error);
• }
• }
• demoPromise();
Sr.no Promise Async/Await
Promise has 3 states – resolved, rejected and It does not have any states. It returns a promise either
2.
pending. resolved or rejected.
Error handling is done using .then() and Error handling is done using .try() and .catch()
4.
.catch() methods. methods.