How to unset a variable in MongoDB shell?


Use delete operator to unset variable in MongoDB shell. Following is the syntax −

delete yourVariableName;

Let us now implement the above syntax in order to unset variable in MongoDB shell. First, print the variable name −

> customerDetail;

This will produce the following output −

2019-05-08T22:29:17.361+0530 E QUERY [js] ReferenceError: customerDetail is not defined :
@(shell):1:1

Now you can set a value in the above variable. Following is the query −

> customerDetail={"CustomerFirstName":"Chris"};

This will produce the following output −

{ "CustomerFirstName" : "Chris" }

Following is the query to show the value of a variable −

> customerDetail;

This will produce the following output −

{ "CustomerFirstName" : "Chris" }

Following is the query to unset a variable in MongoDB shell −

> delete customerDetail;

This will produce the following output −

true

Let us verify the variable exists or not. If it does not exist then you will get an error otherwise, a value −

> customerDetail;

This will produce the following output −

2019-05-08T22:30:35.530+0530 E QUERY [js] ReferenceError: customerDetail is not defined :
@(shell):1:1

Updated on: 30-Jul-2019

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements