How to insert a boolean field in MongoDB?

MongoDBDatabaseBig Data Analytics

Since boolean has two values: true and false, therefore, use true or false keyword in MongoDB. Let us create a collection with documents −

> 
db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"David","isMarried":false,"Salary":56000}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e3e344003d395bdc2134708")
}
> db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"Bob","isMarried":true,"Salary":60000}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e3e344d03d395bdc2134709")
}
> db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"Chris","isMarried":false,"Salary":78000}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e3e345a03d395bdc213470a")
}
> db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"Mike","isMarried":true,"Salary":17000}]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e3e346f03d395bdc213470b")
}

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

> db.demo215.find();

This will produce the following output −

{ "_id" : ObjectId("5e3e344003d395bdc2134708"), "EmployeeDetails" : [ { "EmployeeName" : "David", "isMarried" : false, "Salary" : 56000 } ] }
{ "_id" : ObjectId("5e3e344d03d395bdc2134709"), "EmployeeDetails" : [ { "EmployeeName" : "Bob", "isMarried" : true, "Salary" : 60000 } ] }
{ "_id" : ObjectId("5e3e345a03d395bdc213470a"), "EmployeeDetails" : [ { "EmployeeName" : "Chris", "isMarried" : false, "Salary" : 78000 } ] }
{ "_id" : ObjectId("5e3e346f03d395bdc213470b"), "EmployeeDetails" : [ { "EmployeeName" : "Mike", "isMarried" : true, "Salary" : 17000 } ] }
raja
Published on 30-Mar-2020 12:37:10
Advertisements