Basic MongoDB Commands Operations

Share via:
Basic MongoDB Commands Operations

 

👉👉👉Please check the below video

1 – To shows all the databases

Atlas atlas-dsrabt-shard-0 [primary] myFirstDatabase> show dbs

2 – To use the database called sample_airbnb
Atlas atlas-dsrabt-shard-0 [primary] myFirstDatabase> use sample_airbnb

3 – To show all the collections in a database
Atlas atlas-dsrabt-shard-0 [primary] sample_guides> show tables

4 – To get the count of documents in a collection called movies
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.moviess.count()

5 – Another way to get the document count from collection called moviess
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.moviess.countDocuments()

6 – Another way to get the document count from collection called movies
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.moviess.estimatedDocumentCount({})

7 – Command to create a collection (uncapped) called newcollection
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.createCollection(‘newcollection’)

8 – Command to create a capped collection called books
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.createCollection(‘books’,{capped:true,size :15000,max: 4000})
>>here SIZE referes to the memory consumed
>>MAX referes to the number of documents that the collection can accomidate.

9 – To check the specific collection is capped or uncapped
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.newcollection.isCapped()
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.books.isCapped()

10 – To show all the collections in a database
Atlas atlas-dsrabt-shard-0 [primary] sample_training> show collections

11 – To get all the collections in an array format whose collection names are seperated by comma
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.getCollectionNames()

12 – To get the information about every collection in the database, like name,type,to which field index is used
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.getCollectionInfos()

13 – To get more information about the collections
Atlas atlas-dsrabt-shard-0 [primary] sample_training> db.books.stats()

14 – To get the size of a collection in bytes
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.users.dataSize()

15 – To get the total indexes on specified collection
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.users.getIndexes()

16 – To get all the distinct values of collection called movies whose values are in the year in array format
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.movies.distinct(“year”)

17 – To get all the indexes from a collection
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.movies.getIndexes()

18 – To create an index on a field in a collection
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.movies.createIndex({“cast”:1})

19 – To remove the index on specified field in a collection
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.movies.dropIndex({“cast”:1})

20 – To get the total size used by all the indexes in a collection
Atlas atlas-dsrabt-shard-0 [primary] sample_mflix> db.movies.totalIndexSize()

 

For output of the above commands please check the link below.

👇👇👇👇👇👇👇👇👇
🔗https://github.com/VinodSiram/MongoDB/

 

Author    : Teja

LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment