相关疑难解决方法(0)

在mongoDB中按虚拟字段排序(mongoose)

假设我有一些Schema,它有一个像这样的虚拟字段

var schema = new mongoose.Schema(
{
    name: { type: String }
},
{
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
});

schema.virtual("name_length").get(function(){
    return this.name.length;
});
Run Code Online (Sandbox Code Playgroud)

在查询中是否可以按虚拟字段对结果进行排序?就像是

schema.find().sort("name_length").limit(5).exec(function(docs){ ... });
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,结果很简单没有排序......

sorting mongoose mongodb node.js

9
推荐指数
1
解决办法
6474
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1

sorting ×1