繁体   English   中英

如何反转从 Model.find({}) 返回的项目的顺序;

[英]How can I reverse the order of items returned from Model.find({});

这个命令

Model.find({});

由 ZCCADDEDB567ABAE643E15DCF0974E503Z go 使用,返回 Model 类型的所有模型。 但是,每当我显示返回的 JSON 数组时,它的顺序是相反的。

我可以向后循环遍历数组,但很好奇是否有办法让 ZCCADDEDB567ABAE643E15DCF0974E503Z 返回数组,以便我添加的最后一个 model 将是第一个返回的。

谢谢。

您需要在发布日期使用排序。 我是怎么做的

 vObj
      .find()
      .sort({ publishedAt: "-1" })
      .limit(50)
      .then((videos) => {
        //skip it 
      })

您需要在 model 中添加时间戳。我就是这样做的。

var videoSchema = new mongoose.Schema({
    title: { type: String, required: true, },
    description: String,
    imageurl:{type:String,required:true,},
    publishedAt:{type:String,required:true,},

},{ timestamps: true });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM