由于布尔值具有两个值:true和false,因此,请在MongoDB中使用true或false关键字。让我们创建一个包含文档的集合-
> 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") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo215.find();
这将产生以下输出-
{ "_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 } ] }