Microsoft SQL Server Certification Course
- 5k Enrolled Learners
- Weekend
- Live Class
MongoDB is trending right now. Starting from small scale startups, all the way to big organizations, everyone has started using it, therefore making this platform worth exploring. If you are new to the world of MongoDB and still getting the hang of using it, this article is for you. In this article, we will share the most popular MongoDB commands you can use on this platform to make your life easier and your coding process much more efficient.
Before we share the most popular commands of MongoDB, here is a small introduction to the platform.
MongoDB is an open-source relational database management system that was first introduced in the year 2009. It shares a lot of features with MySQL and brings to the table new enhancements and additional capabilities that contribute to its widespread popularity.
Some of the companies which use MongoDb as their primary resource include the likes of HootSuite, Sony, and Zendesk just to name a few.
Mongo: This is one of the most common commands used in MongoDB. When used, you are asking the platform to connect to localhost on default port 27017.
Mongo <host>/<database>: This command is used when you want the platform to connect to a particular database. An example of this command in action can be, mongo 10.121.65.58/mydb.
Mongo –host <hostname or ip address> –port <port no>: If you want to connect to a remote host using a specified port, then you need to use this command. An example of this command in action can be, mongo –host 10.121.65.23 –port 23020.
use <database name>: If at any point in time, you need to switch between existing databases, use this command. Example, use mydb.
Db: If you need to view the current database you are using, make use of this command.
Help: Similar to other platforms, MongoDB too comes with a built-in help window and in order to use it, run this command. Example, help
load(<filename>): If you need to execute or run a JavaScript file at any point in time, make use of this command. Example, load (myscript.js).
db.help(): If you need help with using Db methods, then you can make use of this command. Example, db.help().
Now that you are aware of the basic commands you can use in MongoDB, here are some of the most popular show commands.
show collections: If you need to view all the collections in the current database, then make use of this command. Example: show collections.
show dbs: In the midst of programming, if you need to view the current database being used then use this command. Example: show dbs.
show roles: Within each database, there are different roles. In order to view all these roles, make use of this command. Example: show roles.
CRUD in MongoDB is the industry wide accepted acronym for Create, Read, Update and Delete. As you know, read and write operations can be performed simultaneously in the MongoDB platform and in order to achieve it, use the following commands.
db.collection.insertMany([ <document1>, <document2>, … ]): If you need to insert multiple documents within an already existing collection, then make use of this command. Example, db.books.insertMany( [{“isbn”: 9780198321668, “title”: “Romeo and Juliet”, “author”: “William Shakespeare”, “category”: “Tragedy”, “year”: 2008}, {“isbn”: 9781505297409, “title”: “Treasure Island”, “author”: “Robert Louis Stevenson”, “category”: “Fiction”, “year”:2014}]).
db.collection.insert( <document> ): If you need to insert a single new document into an already existing collection, then make use of this command. Example, db.books.insert({“isbn”: 9780060859749, “title”: “After Alice: A Novel”, “author”: “Gregory Maguire”, “category”: “Fiction”, “year”:2016}).
db.collection.find(<query>): If you need to find a specific document within a collection by using a field value condition, then use this command. Example, db.books.find({“title”:”Treasure Island”}).
db.collection.find(): If you need to find all the documents in an already existing collection, then make use of this command. Example, db.books.find().
db.collection.findOne(<query>, <projection>): If you need to find the first document that matches the query you have given, then make use of this command. Example: db.books.findOne({}, {_id:false}).
db.collection.find(<query>, <projection>): If you need to find some specific fields of a document in a collection, then you can make use of this command. Example: db.books.find({“title”:”Treasure Island”}, {title:true, category:true, _id:false}).
db.collection.update(<query>, <update>): If you need to remove certain in an existing document, by matching a query then you can make use of this command. Example: db.books.update({title : “Treasure Island”}, {$unset : {category:””}}).
db.collection.update(<query>, <update> ): If you need to update some specific fields of a document that match the given query, then make use of this command. Example: db.books.update({title : “Treasure Island”}, {$set : {category :”Adventure Fiction”}}).
db.collection.remove(<query>, {justOne:true}): If in a certain situation, you need to delete a single document that matches your query then use this command. Example: db.books.remove({title :”Treasure Island”}, {justOne:true}).
db.collection.update(<query>, <update>, {multi:true} ): If you need to delete certain fields of all the documents that match your query, then use this command. Example: db.books.update({category : “Fiction”}, {$unset : {category:””}}, {multi:true}).
db.collection.remove({}): If you need to delete all the documents in a collection, irrespective if they match your query or not, then use this command. Example: db.books.remove({}).
db.collection.remove(<query>): If you need to delete all the documents that match a certain query, then make use of this command. Example: db.books.remove({“category” :”Fiction”}).
Conclusion
Similar to other relational database management systems, MongoDB too contains a lot of commands that come in handy in day to day use. Depending upon your use case, make use of any or all of the commands shared above.
If you wish to learn Microsoft SQL Server and build a career in the relational databases, functions, and queries, variables etc domain, then check out our interactive, live-online SQL Training Course here, which comes with 24*7 support to guide you throughout your learning period.
Got a question for us? Mention them in the comments section and we will get back to you or join Mongodb certification course online.
Course Name | Date | Details |
---|---|---|
MongoDB Certification Training Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
edureka.co