亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號