为了账号安全,请及时绑定邮箱和手机立即绑定

在 mongodb 中使用 where

在 mongodb 中使用 where

陪伴而非守候 2021-11-25 15:24:57
我正在研究一个节点 + mongodb 项目。我想过滤所有活动配置文件。这是我的代码。  async getAllProfiles() {    const profiles = await Profile       .find({ $where: () => activeProfile: true  })       .populate('user');    return profiles;   }这不会按预期工作。我该如何解决这个问题?
查看完整描述

2 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

你需要用这个运行 $where 。 https://docs.mongodb.com/manual/reference/operator/query/where/#example


  async getAllProfiles() {

    const profiles = await Profile

      .find({$where:"this.activeProfile==true"})

      .populate('user');

    return profiles;

  }

但是不是使用 $where 你可以在 find 中运行 cond,会更快


  async getAllProfiles() {

    const profiles = await Profile

      .find({activeProfile: true})

      .populate('user');

    return profiles;

  }


查看完整回答
反对 回复 2021-11-25
?
喵喔喔

TA贡献1735条经验 获得超5个赞

const getAllProfiles = () => {

return new Promise((resolve, reject) => {

    Profile.find({activeProfile:true },(profileErr,profileRes)).populate('user').exec((profileErr,profileRes)=>{

        if (profileErr) {

            console.log('profileErr: ', profileErr);

            reject({ status: 500, message: 'Internal Server Error' });

        } else {

            resolve({ status: 200, message: 'User Profile fetch Successfully.', data: profileRes })

        }

    });

});

}

它工作得很好!


查看完整回答
反对 回复 2021-11-25
  • 2 回答
  • 0 关注
  • 198 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信