mongoDB
mongoDB
MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database.
CREATE DATABASE
MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn't exist, otherwise it will return the existing database
use DATABASE_NAME
>use myprofile
switched to myprofile
To check your currently selected database, use the command db
>db
myprofile
If you want to check your databases list, use the command show dbs.
>show dbs
data 0.00000GB
music 0.00000GB
Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.
>db.movie.insert({"name":"pirates of carabbean"})
DROP DATABASE
MongoDB db.dropDatabase() command is used to drop a existing database.
db.dropDatabase()
>use myprofile
switched to myprofile
>db.dropDatabase()
>{ "dropped" : "myprofile" , "ok" : 1 }
CREATE COLLECTION
MongoDB db.createCollection(name, options) is used to create collection.
>use myprofile
switched to myprofile
>db.createCollection("bio")
{ "ok" : 1 }
DROP COLLECTION
MongoDB's db.collection.drop() is used to drop a collection from the database.
db.collection_name.drop
>use myprofile
switched to db myprofile
>show collection
bio
>db.bio.drop()
true
INSERT DOCUMENT
To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method.
>db.COLLECTION_NAME.insert(document)
>db.bio.insert({
_id: objectId(54fsgf68gdgb),
name : 'vinoliya',
age : 21,
gender : 'female'
})
UPDATE DOCUMENT
The update() method updates the values in the existing document.
db.bio.update()
REMOVE DOCUMENT
MongoDB's remove() method is used to remove a document from the collection.
db.bio.remove()
FIND METHOD
In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.
db.COLLECTION_NAME.find({},{KEY:1})
db.bio.find({},{"title":1_id:0})
LIMIT METHOD
To limit the records in MongoDB, you need to use limit() method. The method accepts one number type argument, which is the number of documents that you want to be displayed.
db.COLLECTION_NAME.find().limit(NUMBER)
db.myrow.find({},{"title":1_id:0).limit(2)
{"title":"mongoDB tutorial"}
{"title":"overview"}
SKIP METHOD
Apart from limit() method, there is one more method skip() which also accepts number type argument and is used to skip the number of documents.
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
>db.bio.find({},{"title":1_id:0).limit(1).skip(1)
{"title":"overview"}
>
SORT METHOD
To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.
db.COLLECTION_NAME.find().sort()
AGGREGATION METHOD
db.bio.aggregation(AGGREGATE_OPERATION)
$sum
Sums up the defined value from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avg
Calculates the average of all given values from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$min
Gets the minimum of the corresponding values from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max
Gets the maximum of the corresponding values from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push
Inserts the value to an array in the resulting document.
db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet
Inserts the value to an array in the resulting document but does not create duplicates.
db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first
Gets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.
db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last
Gets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage
db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])
db.bio.update()
REMOVE DOCUMENT
MongoDB's remove() method is used to remove a document from the collection.
db.bio.remove()
FIND METHOD
In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.
db.COLLECTION_NAME.find({},{KEY:1})
db.bio.find({},{"title":1_id:0})
LIMIT METHOD
To limit the records in MongoDB, you need to use limit() method. The method accepts one number type argument, which is the number of documents that you want to be displayed.
db.COLLECTION_NAME.find().limit(NUMBER)
db.myrow.find({},{"title":1_id:0).limit(2)
{"title":"mongoDB tutorial"}
{"title":"overview"}
SKIP METHOD
Apart from limit() method, there is one more method skip() which also accepts number type argument and is used to skip the number of documents.
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
>db.bio.find({},{"title":1_id:0).limit(1).skip(1)
{"title":"overview"}
>
SORT METHOD
To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.
db.COLLECTION_NAME.find().sort()
AGGREGATION METHOD
db.bio.aggregation(AGGREGATE_OPERATION)
$sum
Sums up the defined value from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avg
Calculates the average of all given values from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$min
Gets the minimum of the corresponding values from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max
Gets the maximum of the corresponding values from all documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push
Inserts the value to an array in the resulting document.
db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet
Inserts the value to an array in the resulting document but does not create duplicates.
db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first
Gets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.
db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last
Gets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage
db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])
- $project − Used to select some specific fields from a collection.
- $match − This is a filtering operation and thus this can reduce the amount of documents that are given as input to the next stage.
- $group − This does the actual aggregation as discussed above.
- $sort − Sorts the documents.
- $skip − With this, it is possible to skip forward in the list of documents for a given amount of documents.
- $limit − This limits the amount of documents to look at, by the given number starting from the current positions.
- $unwind − This is used to unwind document that are using arrays. When using an array, the data is kind of pre-joined and this operation will be undone with this to have individual documents again. Thus with this stage we will increase the amount of documents for the next stage.

No comments