要仅获取FALSE值,我们首先创建一个包含文档的集合。字段之一是isEnable,其值为TRUE或FALSE,如下所示
> db.translateDefinitionDemo.insertOne({"_id":10,"StudentName":"Larry","isEnable":true}); { "acknowledged" : true, "insertedId" : 10 } > db.translateDefinitionDemo.insertOne({"_id":20,"StudentName":"Chris","isEnable":false}); { "acknowledged" : true, "insertedId" : 20 } > db.translateDefinitionDemo.insertOne({"_id":30,"StudentName":"Robert","isEnable":true}); { "acknowledged" : true, "insertedId" : 30 } > db.translateDefinitionDemo.insertOne({"_id":40,"StudentName":"Sam","isEnable":false}); { "acknowledged" : true, "insertedId" : 40 } > db.translateDefinitionDemo.insertOne({"_id":50,"StudentName":"Mike","isEnable":true}); { "acknowledged" : true, "insertedId" : 50 }
以下是在find()
方法的帮助下显示集合中所有文档的查询
> db.translateDefinitionDemo.find().pretty();
这将产生以下输出
{ "_id" : 10, "StudentName" : "Larry", "isEnable" : true } { "_id" : 20, "StudentName" : "Chris", "isEnable" : false } { "_id" : 30, "StudentName" : "Robert", "isEnable" : true } { "_id" : 40, "StudentName" : "Sam", "isEnable" : false } { "_id" : 50, "StudentName" : "Mike", "isEnable" : true }
以下是仅在MongoDB中获取FALSE值的查询
> db.translateDefinitionDemo.find({ "_id" : { "$in" : [10, 20, 30,40,50] }, "isEnable" : { "$ne" : true } }).pretty();
这将产生以下输出
{ "_id" : 20, "StudentName" : "Chris", "isEnable" : false } { "_id" : 40, "StudentName" : "Sam", "isEnable" : false }