Javascript 如何修复此sql到mongo转换的展开部分?
我一直在尝试将SQL转换为Mongo,我也尝试过自己尝试,但结果似乎发生了变化(可能是解围的原因),非常感谢您的帮助 也不完全确定如何在mongo中编写order by之后的部分代码。文档似乎没有任何帮助Javascript 如何修复此sql到mongo转换的展开部分?,javascript,mysql,sql,mongodb,mongoose,Javascript,Mysql,Sql,Mongodb,Mongoose,我一直在尝试将SQL转换为Mongo,我也尝试过自己尝试,但结果似乎发生了变化(可能是解围的原因),非常感谢您的帮助 也不完全确定如何在mongo中编写order by之后的部分代码。文档似乎没有任何帮助 select u.id, flair, "displayName", date_part('year', age(birthday)) "age", bio, "codeImgIds", "photoUrl"
select u.id, flair, "displayName", date_part('year', age(birthday)) "age", bio, "codeImgIds", "photoUrl"
from "user" u
left join view v on v."viewerId" = $1 and u.id = v."targetId"
left join view v2 on $2 = v2."targetId" and v2.liked = true and u.id = v2."viewerId"
where
v is null
and u.id != $3
${user.goal === "love" ? user : friendWhere}
and array_length("codeImgIds", 1) >= 1
and "shadowBanned" != true
order by
random()
- (case
when (v2 is not null)
then .2
else -least(current_timestamp::date - u."lastSwipe"::date, 14) / 14.0
end)
- LEAST(u."numSwipesToday", 20) / 20.0
limit 20;
`,
user.goal === "love" ? loveParams : friendParams
我的尝试
const result = await user.findById(req.user.data._id);
user
.aggregate([
{
$lookup: {
from: "views",
pipeline: [{ $match: { viewerId: { $ne: _id } } }],
as: "v",
},
},
{ $unwind: "$v" },
{
$lookup: {
from: "views",
let: { targetId: _id, liked: true },
pipeline: [{ $match: { liked: true } }],
as: "v2",
},
},
{ $unwind: "$v2" },
{
$match: {
$and: [
{ _id: { $nin: [req.user.data._id, v2.viewerId] } },
{ goal: result.goal },
{ shadowBanned: false },
{ genderToShow: result.genderToShow },
],
},
},
])