如何实现MongoDB $ or运算符
在MongoDB中使用$or运算符评估一个或多个表达式。以下是语法-
db.yourCollectionName.find({ $or: [{ "yourFieldName": yourValue1 }, { "yourFieldName": yourValue2} ] } ).pretty();
首先让我们创建一个包含文档的集合-
> db.orOperatorDemo.insertOne({"StudentNames":["John","Carol","Sam"]});{
"acknowledged" : true,
"insertedId" : ObjectId("5cd6b80a6d78f205348bc61b")
}
> db.orOperatorDemo.insertOne({"StudentNames":["Robert","Chris","David"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd6b8266d78f205348bc61c")
}
> db.orOperatorDemo.insertOne({"StudentNames":["John"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd6b8346d78f205348bc61d")
}
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.orOperatorDemo.find().pretty();
这将产生以下输出-
{"_id" : ObjectId("5cd6b80a6d78f205348bc61b"),
"StudentNames" : [
"John",
"Carol",
"Sam"
]
}
{
"_id" : ObjectId("5cd6b8266d78f205348bc61c"),
"StudentNames" : [
"Robert",
"Chris",
"David"
]
}
{
"_id" : ObjectId("5cd6b8346d78f205348bc61d"),
"StudentNames" : [
"John"
]
}
这是对$or运算符语法的查询-
> db.orOperatorDemo.find({ $or: [{ "StudentNames": "Carol" }, { "StudentNames": "John"} ] } ).pretty();
这将产生以下输出-
{"_id" : ObjectId("5cd6b80a6d78f205348bc61b"),
"StudentNames" : [
"John",
"Carol",
"Sam"
]
}
{
"_id" : ObjectId("5cd6b8346d78f205348bc61d"),
"StudentNames" : [
"John"
]
}
以上是 如何实现MongoDB $ or运算符 的全部内容, 来源链接: utcz.com/z/355613.html