的$或操作者执行逻辑OR操作的两个或多个表达式的阵列上。让我们创建一个包含文档的集合-
> db.demo674.insertOne({Name:"Chris",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33604263e90dac943eb") } > db.demo674.insertOne({Name:"David",Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33c04263e90dac943ec") } > db.demo674.insertOne({Name:"Bob",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34204263e90dac943ed") } > db.demo674.insertOne({Name:"John",Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34804263e90dac943ee") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo674.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea3f33c04263e90dac943ec"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5ea3f34204263e90dac943ed"), "Name" : "Bob", "Age" : 21 } { "_id" : ObjectId("5ea3f34804263e90dac943ee"), "Name" : "John", "Age" : 24 }
以下是使用$or运算符获取文档的查询-
> db.demo674.find({$or:[{Name:"David"},{Age:21}]});
这将产生以下输出-
{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea3f33c04263e90dac943ec"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5ea3f34204263e90dac943ed"), "Name" : "Bob", "Age" : 21 }