or
为 $or
条件指定参数;
//member = ["zhangsan", "lisi", "wangwu"];
let filter = {
$or: [
{docKey: "public"},
{owner: "zhangsan"},
{member: "lisi"}
]
}
db.getCollection('docs').find(filter);
- 数据筛选or是或查询
- 满足下面的条件其中之一就可以查询出相关数据
- 如果数据库参数是数组,直接会从数组里面匹配是否包含相关数据 如上:member
and
为 $and
条件指定参数;
let filter = {
$and: [
{docKey: "public"},
{owner: "zhangsan"}
]
}
db.getCollection('docs').find(filter);
直译:
docKey == "public" && owner == "zhangsan"
需要满足枚举的两个条件才能筛选出相应数据;
and与or联合使用
let filter = {
$or: [
{
docKey: "public"
},
{
$and: [{docKey: "private"}, {$or:[{member: "zhangsan"}, {owner: "lisi"}]}]
}
]
}
db.getCollection('docs').find(filter);
直译:
docKey == "public" || (docKey == "private" && (member.indexOf("zhangsan")>-1 || owner=="lisi"))
where
let filter = {
$or: [
{
docKey: "public"
},
{
$where: function(){
return this.docKey == "private" && (this.member.indexOf("zhangsan") > -1 || this.owner == "lisi");
}
}
]
}
db.getCollection('docs').find(filter);
直译:
docKey == "public" || (docKey == "private" && (member.indexOf("zhangsan")>-1 || owner=="lisi"))
或者如下的写法
let filter = {
$or: [
{
docKey: "public"
},
{
$where: `this.docKey == "private" && (this.member.indexOf("zhangsan") > -1 || this.owner == "lisi")`
}
]
}
db.getCollection('docs').find(filter);
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。