MongoDB
MongoDB Feature Card
-
- Queries: It supports ad-hoc queries and document-based queries.
- Index Support: Any field in the document can be indexed.
- Replication: It supports Master–Slave replication. MongoDB uses native application to maintain multiple copies of data. Preventing database downtime is one of the replica set’s features as it has self-healing shard.
- Multiple Servers: The database can run over multiple servers. Data is duplicated to foolproof the system in the case of hardware failure.
- Auto-sharding: This process distributes data across multiple physical partitions called shards. Due to sharding, MongoDB has an automatic load balancing feature.
- MapReduce: It supports MapReduce and flexible aggregation tools.
- Failure Handling: In MongoDB, it’s easy to cope with cases of failures. Huge numbers of replicas give out increased protection and data availability against database downtime like rack failures, multiple machine failures, and data center failures, or even network partitions.
- GridFS: Without complicating your stack, any sizes of files can be stored. GridFS feature divides files into smaller parts and stores them as separate documents.
- Schema-less Database: It is a schema-less database written in C++.
- Document-oriented Storage: It uses BSON format which is a JSON-like format.
- Procedures: MongoDB JavaScript works well as the database uses the language instead of procedures.
Mongo DB Vs RDBMS
MongoDB RDBMS Document-oriented and non-relational database Relational database Document based Row based Field based Column based Collection based and key–value pair Table based Gives JavaScript client for querying Doesn’t give JavaScript for querying Relatively easy to setup Comparatively not that easy to setup Unaffected by SQL injection Quite vulnerable to SQL injection Has dynamic schema and ideal for hierarchical data storage Has predefined schema and not good for hierarchical data storage 100 times faster and horizontally scalable through sharding By increasing RAM, vertical scaling can happen
Database Creation
- MongoDB doesn’t have any methods to create a database. It automatically creates a database when you save values into the defined collection for the first time. The following command will create a database named ‘database_name’ if it doesn’t exist. If it does exist, then it will be selected.
- Command: Use Database_name
Dropping Databases
- The following command is used to drop a database, along with its associated files. This command acts on the current database.
- Command: db.dropDatabase()
Creating a Collection
- MongoDB uses the following command to create a collection. Normally, this is not required as MongoDB automatically creates collections when some documents are inserted.
- Command: db.createCollection(name, options)
- Name: The string type which specifies the name of the collection to be created
- Options: The document type which specifies the memory size and the indexing of the collection. It is an optional parameter.
Showing Collections
- When MongoDB runs the following command, it will display all the collections in the server.
- Command: In shell you can type: db.getCollectionNames()
$in Operator
- The $in operator selects those documents where the value of a field is equal to the value in the specified array. To use the $in expression, use the following prototype:
- Command: { field: { $in: [<value1>, <value2>, … <valueN> ] } }
Projection
- Often you need only specific parts of the database rather than the whole database. Find() method displays all fields of a document. You need to set a list of fields with value 1 or 0. 1 is used to show the field and 0 is used to hide it. This ensures that only those fields with value 1 are selected. Among MongoDB query examples, there is one which defines projection as the following query.
- Command: db.COLLECTION_NAME.find({},{KEY:1})
Date Operator
- This command is used to denote time.
- Command:
Date() – It returns the current date as a string.
New Date() – It returns the current date as a date object.
$not Operator
- $not does a logical NOT operation on the specified <operator-expression> and selects only those documents that don’t match the <operator-expression>. This includes documents that do not contain the field.
- Command: { field: { $not: { <operator-expression> } } }
Delete Commands
- Following are commands which explain MongoDB’s delete capabilities.
- Commands:
collection.remove() – It deletes a single document that matches a filter. db.collection.deleteOne() – It deletes up to only a single document even if the command selects more than one document.
db.collection.deletemany() – It deletes all the documents that match the specified filter.
Where Command
- To pass either a string which has a JavaScript expression or a full JavaScript function to the query system, the following operator can be used.
- Command: $where
The forEach Command
- JavaScript function is applied to each document from the cursor while iterating the cursor.
- Command: cursor.forEach(function)