用戶檔案
這是我的 mongodb 中的用戶檔案,還有許多其他用戶檔案。并有一系列這樣的興趣
myInterests['gaming','reading']
這個陣列將來可以是動態的
我想撰寫一個查詢,我可以在其中獲取具有這 2 個興趣或至少 1 個興趣的用戶的串列,我該如何為此撰寫查詢?
router.get("/", auth, async function (req, res) {
const { interests: myInterests } = req.user;
const users = await User.find({
interests: { $in: [myInterests] },
}).select({ email: 1, interests: 1 });
console.log(users);
});
我已經嘗試過這段代碼,但是這段代碼讓我得到了那些只有這 2 個興趣的用戶的串列。我也想獲得那些至少有 1 個該陣列元素的用戶。
uj5u.com熱心網友回復:
所述myInterests已經是一個陣列。查詢應該是這樣的
interests: {
$in: myInterests,
}
代替
interests: {
$in: [myInterests],
}
否則,它將使用 2D 陣列查找精確匹配項。
作業游樂場 => https://mongoplayground.net/p/D2_JwvBUn3k
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/375411.html
上一篇:我如何從反應中的事件中獲得價值?
