$ mongo
use <db-name>;
db;
db.createCollection("<create-collection>");
db.<collection-name>.insert({name: "Bob", age: 20, desg: "Analytics"}); // Inserting One Document
db.<collection-name>.insertMany([{name: "Alice", age: 30, desg: "Devops"}, {name: "Sue", age: 35, desg: "Full stack"}]);
db.<collection-name>.find();
db.<collection-name>.find().pretty();
db.<collection-name>.distinct("name");
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.
db.<collection-name>.drop();
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)
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)
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)
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)
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)
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> } }
Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.
{ “$match” : { <query> } }
$group is used to group the documents by some specified expression.
{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> } } }
An index in MongoDB is a special data structure that holds the data of few fields of documents on which the index is created.
db.collection_name.createIndex({key: 1 or -1})
Here, '1' represents ascending order of indexing whereas '-1' descending.
db.<collection-name>.getIndexes()
db.<collection-name>.dropIndexes()
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.
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
>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.
D:\set up\mongodb\bin>mongotop 30
#### The above example will return values every 30 seconds.