关于前端:mongoose数据查询orandwhere等用法

50次阅读

共计 1084 个字符,预计需要花费 3 分钟才能阅读完成。

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);

正文完
 0