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: 30-Jul-2019

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements