2

or

$or 条件指定参数;

//member = ["zhangsan", "lisi", "wangwu"];
let filter = {
  $or: [
    {docKey: "public"},
    {owner: "zhangsan"},
    {member: "lisi"}
  ]
}
db.getCollection('docs').find(filter);
  1. 数据筛选or是或查询
  2. 满足下面的条件其中之一就可以查询出相关数据
  3. 如果数据库参数是数组,直接会从数组里面匹配是否包含相关数据 如上: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);

兰俊秋雨
5.1k 声望3.5k 粉丝

基于大前端端技术的一些探索反思总结及讨论