猿问

在 mongodb 中使用 where

我正在研究一个节点 + mongodb 项目。我想过滤所有活动配置文件。这是我的代码。

  async getAllProfiles() {    const profiles = await Profile
      .find({ $where: () => activeProfile: true  })
      .populate('user');    return profiles;
  }

这不会按预期工作。我该如何解决这个问题?


陪伴而非守候
浏览 199回答 2
2回答

繁花如伊

你需要用这个运行 $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;  }

喵喔喔

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 })        }    });});}它工作得很好!
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答