5

node mongoose 查找 find ,当前端没有传过来筛选参数的时候 ,查找条件这块应该怎么...

 2 years ago
source link: https://www.v2ex.com/t/802963
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Node.js

node mongoose 查找 find ,当前端没有传过来筛选参数的时候 ,查找条件这块应该怎么写?

  PeakFish · 2 小时 48 分钟前 · 77 次点击

上代码,下面的 id 字段 和 status 字段,如果前端没有传过来这个字段,那就 去掉筛选 或 者查询全部。这块应该怎么写?

const findList = await userModel
  // mongoose 的查找方法
  .find({
    nickname: { $regex: nicknameReg },
    _id: id ? ObjectId(id) : '查询字段的所有状态 或者 不查询这个字段, 这块需要怎么写?',
    status: status ? status : '查询字段的所有状态 或者 不查询这个字段, 这块需要怎么写?'
  })

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK