mongoose数据查询or、and、where等用法

浅夏晴空

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);
阅读 385

大前端技术栈
大前端技术汇总
avatar
浅夏晴空
前端开发工程师

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

4.5k 声望
3.1k 粉丝
0 条评论
你知道吗?

avatar
浅夏晴空
前端开发工程师

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

4.5k 声望
3.1k 粉丝
宣传栏