Skip to content

Cheatsheet-lang/mongoDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

MongoDB Cheatsheet

General Commands

Opens the monogDB shell

$ mongo

Commands

Basics

Create and Use Database

use <db-name>;

See Default database

db;

Create Collection

db.createCollection("<create-collection>");

Inserting One Document

db.<collection-name>.insert({name: "Bob", age: 20, desg: "Analytics"});         // Inserting One Document

Inserting Many Documents

db.<collection-name>.insertMany([{name: "Alice", age: 30, desg: "Devops"}, {name: "Sue", age: 35, desg: "Full stack"}]);

Display all Documents

db.<collection-name>.find();

Display Documents in JSON Format

db.<collection-name>.find().pretty();

Display Distinct

db.<collection-name>.distinct("name");

Update Collection

db.<collection-name>.update({ refKey : "refValue" }, { $set : { changeKey : "changeValue"}})

refKey and refValue refers to the key and value pair from the document which needs to be updated. changeKey and changeValue refers to the actual content which needs to be updated.

Drop Collection

db.<collection-name>.drop();

Operations

Projection

When we want to retrieve only specific number of fields from a set of documents, this method is used. The fields(keys) we wish to retrieve are passed in find method with true Boolean condition. If we don’t want to retrieve _id we can hide it by giving false Boolean value to it.

db.<collection-name>.find({}, key:1, _id:0)

Limit

If we want to retrieve first specific number of document from a collection this method is used. The number of elements we wish to retrieve is passed in the limit method.

db.<collection-name>.find({}, key:1, _id:0).limit(number)

Skip

Imagine skipping a particular number of documents from a collection while retrieving data. In such cases, skip method is useful.

Query:  db.<collection-name>.find({}, key:1, _id:0).skip(number)

Sort

As the name suggests this method is useful when we wish to retrieve data in a particular manner. If we want to retrieve data an ascending order, 1 is passed or -1 if descending order.

db.<collection-name>.find().sort(refKey:num)

Projection Methods

Aggregation :

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. For the aggregation in MongoDB, you should use aggregate() method.

db.<collection-name>.aggregate(Aggregate_operation)

$project method :

Passes along the documents with only the specified fields to the next stage in the pipeline. This may be the existing fields from the input documents or newly computed fields.

{ “$project”: { <specification> } }

$match method :

Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

{ “$match” : { <query> } }

$group method :

$group is used to group the documents by some specified expression.

{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> } } }

Indexing

An index in MongoDB is a special data structure that holds the data of few fields of documents on which the index is created.

Apply a new indexing

db.collection_name.createIndex({key: 1 or -1})

Here, '1' represents ascending order of indexing whereas '-1' descending.

Retrieve the index

db.<collection-name>.getIndexes()

Drop the existing index

db.<collection-name>.dropIndexes()

Replication

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers.

Set Up a Replica Set

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

Add Members to Replica Set

>rs.add(HOST_NAME:PORT)

### Sharding
Sharding is the process of storing data records across multiple machines and it is MongoDB's approach to meeting the demands of data growth.

### Create Backup
#### Dump MongoDB Data

mongodump


#### Restore data

mongorestore

### Deployment
#### When you are preparing a MongoDB deployment, you should try to understand how your application is going to hold up in production.

mongostat

#### This command checks the status of all running mongod instances and return counters of database operations.

#### To run the command, start your mongod instance. In another command prompt, go to bin directory of your mongodb installation and type mongostat.

D:\set up\mongodb\bin>mongostat


mongotop

#### This command tracks and reports the read and write activity of MongoDB instance on a collection basis. By default, mongotop returns information in each second, which you can change it accordingly.
#### To run the command, start your mongod instance. In another command prompt, go to bin directory of your mongodb installation and type mongotop.

D:\set up\mongodb\bin>mongotop

#### To change mongotop command to return information less frequently, specify a specific number after the mongotop command.
```mongo
D:\set up\mongodb\bin>mongotop 30

The above example will return values every 30 seconds.

Running the commands

To run a command against the current database, use

db.runCommand( { <command> } )

To run an administrative command against the admin database, use

db.adminCommand( { <command> } )