【问题标题】:Sails.js: How to find records based on values in associated collection?Sails.js:如何根据关联集合中的值查找记录?
【发布时间】:2019-01-30 02:32:50
【问题描述】:

我正在寻找一种使用 Sails.js Waterline 进行子查询的方法。该记录与 Charge 模型有关联。

我希望这样的东西可以工作,但这似乎不受支持:

var topRecords = await Record.find({'charge.paid':true});

我得到的最接近的是:

var topRecords = await Record.find().populate('charge',{paid:true});

但问题是它仍然返回所有记录,只是不填充与填充 where 语句不匹配的记录。

我不能先搜索费用的原因是我想根据记录中的一个值对数据进行排序。

【问题讨论】:

    标签: node.js mongodb sails.js waterline


    【解决方案1】:

    您可以获取Charges,然后使用.map 从那里获取记录。

    const topCharges = await Charge.find({ paid: true }).populate('records');
    const topRecords = topCharges.map(charge => charge.record);
    

    【讨论】:

    • 您将如何根据 Records 集合或 CreatedAt 中的点赞数对 topRecords 进行排序?
    • 我已经用lodash实现了排序!忽略我的评论
    猜你喜欢
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多