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

标签 javascript node.js mongodb sorting mongoose-populate

我想根据时间检索成员(member)最近提交的文章。成员架构具有已提交文章的 _id 数组。 以下是成员和文章的架构 -

成员架构

    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;

时间存储在文章架构中,而不是存储在成员架构数组中。我想首先获得最近文章中的输出。

下面是 Mongoose 填充操作,具有排序功能,但不起作用。

//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});
    })
});

最佳答案

//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});
    })
});

尝试上面的方法。希望这会有所帮助。

关于javascript - 如何使用 mongoose 对 Nodejs 中填充结果的子文档数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50627403/

相关文章:

javascript - Monaco 编辑器如何添加自定义语言解析器和语法验证

javascript - 禁用透明 header-div 后面的 Bootstrap 按钮的点击

node.js - 允许使用 restify 的选项方法 - 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段授权

node.js - 检查 NodeJs 中的互联网连接

node.js - 数组 Mongoose 中的默认值

mongodb - 如何使用 MongoDB 在 csv 中将分隔符从逗号更改为 #

javascript - ASCIIMath:获取渲染表达式的 TeX 值的 JS 方法?

javascript - AngularJS:ngSwipeLeft 上的函数调用

javascript - AssertionError [ERR_ASSERTION] : handler (func) is required in mongodb

mysql - MongoDB 中的 Atomic 转账