Use Result from MongoDB in Shell Script



Let us first create a collection with a document −

>db.useResultDemo.insertOne({"StudentFirstName":"Robert"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cda70f5b50a6c6dd317adcd")
}

Display all documents from a collection with the help of find() method −

> db.useResultDemo.find();

Following is the output −

{ "_id" : ObjectId("5cda70f5b50a6c6dd317adcd"), "StudentFirstName" : "Robert" }

Here is the query to use result from MongoDB in shell script with var keyword −

> var studentName=db.useResultDemo.findOne({},{_id:0});
> studentName

Following is the output −

{ "StudentFirstName" : "Robert" }
Updated on: 2019-07-30T22:30:26+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements