聚集数组和排序,猫鼬

时间:2019-07-01 21:59:54

标签: node.js mongodb mongoose

我希望能够根据我的like字段(一个对象数组)进行排序。

这是我现在设置的:

       const report = await Report.find({createdAt: { $gt: startDate, $lt: Date() }}, null, {sort: ...})
            .populate({path: 'like'})
            .populate({
                path: 'player',
                populate: [{ path: 'team' },
                {
                    path: 'team',
                    populate: {
                        path: 'league'
                    }
                }
            ]
            })

这是我的报告架构:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

let Report = new Schema({
    title: {
        type: String
    },
    summary: {
        type: String
    },
    analysis: {
        type: String
    },
    source_title: {
        type: String
    },
    source_link: {
        type: String
    },
    author: {
        type: String
    },
    like: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Like'
    }],
    player: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'Player'
    }
}, { timestamps: true })

module.exports = mongoose.model('Report', Report)

这是我的“喜欢模式”的样子:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

let Like = new Schema({
    value: {
        type: Number
    },
    _report: {
        type: String,
        ref: 'Report'
    }
}, { timestamps: true })

module.exports = mongoose.model('Like', Like)

我本质上是希望将特定报告的所有喜欢加起来,然后按最喜欢的报告对报告进行排序。

0 个答案:

没有答案