首页 > 解决方案 > mongoDB中的条件过滤

问题描述

我可以有条件地使用 $and 过滤器之一,即。当满足某些条件时,我想使用过滤器,否则不要使用过滤器:

Collection.find({
  $and: [
    { date  },
    { room: [isConditionMet] ? room : [do not use room filter at all] },
  ]
}

是否可以在 mongo 查询级别?

标签: mongodb

解决方案


不是和使用$or一样吗?

Collection.find({
  $and: [
    { date: ...  },
    { $or: [
      {isConditionNOTmet},
      {room: ... }
    ]}
  ]
})

推荐阅读