首页 > 解决方案 > MongoDB find $and with value of any

问题描述

How can i use find with $and with some value all?

Example

let { city, zanr, date } = req.body;

const selectedEvent = await Events.find({
  $and: [{ address: city}, { type: zanr }, { date: datum }],
});

Because in req.body can be the value "all" thats mean i dont need sometimes filter.

Sometimes can be address: any

标签: javascriptmongodbmongoosemongodb-querynosql

解决方案


if city is all remove {address:city} from and array or use {address:{$regex:'.*.'}}

 let { city, zanr, date } = req.body;

    const selectedEvent = await Events.find({
      $and: [ {address:{$regex:'.*.'}},{ type: zanr }, { date: datum }],
    });

推荐阅读