【发布时间】:2016-10-29 20:37:38
【问题描述】:
我正在尝试在 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 } ]
关于为什么它没有正确排序的任何想法?我一直在查看其他工作和示例,但看不出我哪里出错了。
【问题讨论】:
-
排序和索引不是一回事。 stackoverflow.com/questions/25449570/…
-
@BrianGlaz 我明白这一点,但我想索引我的数据,以便每次保存新条目时,它都会按分数降序保存,这样我将始终拥有前 10 个分数我的数据库排名前 10 位。
标签: node.js mongodb sorting indexing mongoose