mongoose里面,有些参数不一定传,怎么写model.find?

mongoose里面,获取历史列表,collect这个参数,不传就获取所有数据
传了query.collect就获取已收藏的列表,请问要怎么写?

    const model = db.model("history_list", {
        openid: String,
        collect: Number
    })


    // 方法 --->
    model.find({ openid: query.openid }).then(res => {
        callback(res)
    }).catch(err => {
        return false
    })

不会要写2个model.find(),根据有没有参数,IF判断进入哪个model.find吧,这也太傻了

阅读 1.4k
1 个回答
const query = {
  collect: query.collect!=null ? query.collect : { $ne: null }
};
model.find(query)
推荐问题