如何使用mongoose对Nodejs中填充结果的子文档数组进行排序

时间:2018-05-31 15:05:34

标签: javascript node.js mongodb sorting mongoose-populate

我想根据时间以最近的顺序检索由成员提交的文章。具有提交文章的_id数组的成员模式。 以下是会员的模式和文章 -

会员Schama

    const mongoose = require('mongoose');
        const Schema = mongoose.Schema;
        const MemberSchema = new Schema({
            uid:{
                type:String,
                required:[true,'Must Required']
            },
            name:{
                type:String,
                default:[true,'Anonymous']
            },
            email:{
                type:String,
                required:[true,'Email Must Required']
            },

            profileimg:{
                type:String ,
                required:[true,'Profile Image Must Required']
            },
            notes:{
                type:String,
                default:[true,'Write your Notes Here']
            },
            country:{
                type:String,
                default:[true,'America']
            },
            profession:{
                type:String,
                default:[true,'Developer']
            },
            experience:{
                type:String,
                default:[true,'5']
            },
            article:[{_id:{type : mongoose.Schema.Types.ObjectId,ref : 'Article'}}]



        });

        const Member = mongoose.model('member',MemberSchema);

        module.exports = Member;

文章架构

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ContentCardSchema = new Schema({
    cardtitle : String,
    cardbody : {
        type:String
    }
});
const ArticleSchema = new Schema({
    _id:mongoose.Schema.Types.ObjectId,
    author:{
        type: mongoose.Schema.Types.ObjectId, ref: 'member' ,
        required:[true,'UID Must Required']
    },
    status:{
        type:String,
        default:'PENDING'
    },
    category:{
        type:String,
        default:'Other'
    },
    title:{
        type:String,
        default:'Anonymous'
    },
    body:[ContentCardSchema],
    thumbimg:{
        type:String,
        required:[true,'Thumb Image Must Required']
    },
    date: {
        type: Date,
        default:Date.now
    }



});

const Article = mongoose.model('Article',ArticleSchema);

module.exports = Article;

时间存储在Article Schema中,而不是存储在Member Schema数组中。我想先在最近的文章中获得输出。

以下是Mongoose Populate Operation with sort但不工作。

//Get Article Submitted by their Author using uid
router.get('/author/:uid',(req,res,next)=>{
    Member.findOne({uid:req.params.uid},'article').populate('article._id','title category thumbimg')
    .exec()
    .then((result)=>{
        res.status(200).json(result);
    })
    .catch(err=>{
        res.send(404).json({error:err});
    })
});

1 个答案:

答案 0 :(得分:-1)



//Get Article Submitted by their Author using uid
router.get('/author/:uid',(req,res,next)=>{
    Member.findOne({uid:req.params.uid},'article')
    .populate({path:'article._id',select: 'title category thumbimg', options: {sort : {yourfieldname :1}})
    .exec()
    .then((result)=>{
        res.status(200).json(result);
    })
    .catch(err=>{
        res.send(404).json({error:err});
    })
});




尝试以上。希望这有帮助。