我正在尝试在mongoose中创建排行榜,但在按升序对分数模式进行索引时遇到了问题。
这是我的代码:
db.once('open', function callback () {
console.log('Successfully connected to MongoDB');
var scoresSchema = new Schema ({
score: Number,
user: String
}, {autoIndex: false});
scoresSchema.index({ user: 1, score: -1 });
StatScore = mongoose.model('Score', scoresSchema);
});
并且它的输出如下:
[ { _id: 57715497860521f404cfebf0, score: 87, user: 'seth', __v: 0 },
{ _id: 577157151f39c2320548e6e5, score: 99, user: 'seth', __v: 0 },
{ _id: 57716a4613e701890608d18a, score: 97, user: 'seth', __v: 0 },
{ _id: 57716a7413e701890608d18b, score: 135, user: 'john', __v: 0 } ]
有什么想法不排序的原因吗?我一直在查看其他的工作和例子,但是找不到我的错误在哪里。