What is Mongo Shell?
Mongo Shell is a practice field for beginners and can be used to perform advance administrative operation as well. Database and collections can be created, accessed, modified and deleted using MongoDB shell. Mongo Shell is a component of MongoDB Distributions. It is an interactive JavaScript interface to MongoDB.![]() |
What is Mongo Shell? |
Mongo shell can be use to perform the CRUD operations. Once you have installed and have started MongoDB, connect the mongo shell to your running MongoDB instance.
MongoDB supports AD hoc queries like field, range query, and regular expression searches. Queries can return specific fields of documents and also include user-defined JavaScript functions. Queries can also be configured to return a random sample of results of a given size.
db.cars.find({color:"red"});
//Result : { "_id" : ObjectId("5d53066a520ea88ac000e982"), "name" : "Opel", "color" : "red" }
How to start MongoDB Shell?
To start MongoDB you have to install MongoDB first on your PC. You can easily setup MongoDB on your pc by taking reference of the below “How to Link”.MongoDb Tutorial : How to Install MongoDB?
If you have Window OS 32-bit then you need to go MongoDB installed program file location (by default – “C:\Program Files\MongoDB\Server\3.2\bin\”) and double-click on mongod.exe to start the MongoDB instance and after that find another file named mongo.exe in same directory, double-click on that and MongoDB shell will be started.
![]() |
How to Start MongoDB Shell in Window7 32-Bit - progrramers.com |
Else if you have Window OS 64-bit you need to go MongoDB installed program file location (by default – “C:\Program Files\MongoDB\Server\4.0\bin\”) and double-click on mongo.exe, MongoDB shell will be start along with the MongoDB instance.
Progrramers Recommends : Best Practice
Set the Path for MongoDB instance and MongoDB Shell in Windows Environment Variables.Store a JavaScript Function on the Server - MongoDB Stored Procedure Equivalent (MongoDB Procedure Operations)
Store Procedure is the best way to extract faster performance from database programs because it minimizes the gap between data and database program. A stored procedure is a set of database statements that has been created and stored in the database. A stored procedure accepts input parameters from server side script and performs the task so that a single procedure can be used over the network by several clients using different input data.There is nothing called store procedure in MongoDB but MongoDB provides a highly efficient mechanism to perform the task like store procedure. MongoDB documents can store the JavaScript functions as data which can accept the parameters and execute the function accordingly.
In MongoDB JavaScript functions can be saved in system.js document and execute later when required. For reference check below example:
db.system.js.save(
{
_id:"getSum",value:function(a, b){
return a + b
}
}
);
db.eval("return getSum(2, 6)");
// Result : 8
CRUD operations in MongoDB Shell
Like other database program MongoDB shell can be used to perform CRUD operations. CRUD operations refer to the basic functions like Create, Read (Find), Update and Delete.MongoDB Shell: Create or insert operation
Create or insert operation adds new document inside a collection. If there is no collection found with specified name then first it creates the collection with specified name and then inserts a new document inside a collection.MongoDB shell provides following Methods to insert document in collection.
- collectionname.insert() – insert one or more document at same time
- collectionname.inserOne() – insert one document at a time
MongoDB Shell: Read or Find Operation
Read or Find operation retrieves the data from a collection. Find operation can use regex to find the specific pattern of data and execute the result accordingly. MongoDB shell provides following methods to find the document from a collections.- collectioname.find() – find whole document available in a collection
- collectioname.findOne() – find top most document available in result in a collection
- collection.find().forEach(printjson) – find whole document available in a collection and output the result in JSON oriented format
- collection.find().pretty() – find whole document available in a collection and output the result in JSON oriented format
MongoDB Shell: Update Operation
Update operation can modifies the exiting document in a collection. You can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as read operations. Update operation can also use regex to find the specific pattern of data and execute the result accordingly. Like mentioned above in read operation values are case sensitive in specified criteria.- collection.updateOne() – Update one record as per specified criteria
- collection.updateMany() – Update many record as per specified criteria
- collection.replaceOne() – Replace one record as per specified criteria
Friends this my new post explaining the MongoDB Shell. If you have any question then feel free ask in the comment sections.
ReplyDelete