こんにちは、私はそれが作成された逆順で私のクエリを返そうとしています。ここでhttp://mongoosejs.com/docs/api.html#types_array_MongooseArray.sort逆順で並べ替え
は私のスキーマです:
ドキュメントはsortメソッドを使用する方法についてはほとんど不明である例えば
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;
let PostSchema = new Schema({
title : String,
description: String,
image : String,
tags : [String],
original_poster: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
date: {
type: Date,
default: new Date()
}
})
module.exports = mongoose.model('Post',PostSchema);
私が実行した、
db.posts.find().sort({date:-1}).pretty()
私のモデルが「投稿」モデルで、最初の投稿が「こんにちは世界」で、2番目の投稿が「これは投稿」でした。
['this is a post', 'hello world']
しかし、私が実際に見ていますがあなたは、そのキーで、あなたのスキーマとソートに作成タイムスタンプを追加する必要が['hello world','this is a post']
スキーマには 'date'フィールドはありません。すべての文書で同じように '未定義の 'フィールドでソートしています。 –