节点 - Mongoose 3.6 - 使用填充字段对查询进行排序 [英] Node - Mongoose 3.6 - Sort query with populated field

查看:27
本文介绍了节点 - Mongoose 3.6 - 使用填充字段对查询进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行远程网格使用的查询,因此我必须处理每个字段的排序(asc、desc).

I am trying to do a query used by a remote grid, so I will have to handle sort (asc, desc) on every fields.

以下是架构:

var customerSchema = new mongoose.Schema({
status: {type: mongoose.Schema.Types.ObjectId, ref: 'Status'},
contact: {type: mongoose.Schema.Types.ObjectId, ref: 'Contact'}
}, { collection: 'Customer' });

customerSchema.virtual('contactName').get(function () {
   if (this.contact && this.contact.get) {
       return this.contact.get('firstName') + ' ' + this.contact.get('lastName');
   }

   return '';
});

customerSchema.virtual('statusName').get(function () {
   if (this.status && this.status.get) {
       return this.status.get('name');
   }

   return '';
});

customerSchema.set('toJSON', { virtuals: true });
customerSchema.set('toObject', { virtuals: true });
mongoose.model('Customer', customerSchema);

// STATUS
var statusSchema = new mongoose.Schema({}, { collection: 'Status' });
mongoose.model('Status', statusSchema);

// CONTACT
var contactSchema = new mongoose.Schema({
    firstName: String,
    lastName: String
}, { collection: 'Contact' });
mongoose.model('Contact', contactSchema);

这里是查询:

exports.customerList = function (predicate ,callback){
if (!predicate) predicate = 'name';
var Customers = mongoose.model( 'Customer' );
    
Customers.find()
    .select('name phone address status contact contactName statusName')
    .populate('status', 'name')
    .populate('contact', 'firstName lastName')
    .sort(predicate)
    .exec(callback);
};

查询在按姓名"(即 Customer.name)或地址"(Customer.address)排序时有效,但在contact.firstName"(应为 Customer.contact)时无法正常工作.firstName).

The query is working when sorting on 'name' (so Customer.name) or 'address' (Customer.address) but can't get it to work when it is 'contact.firstName' (should be Customer.contact.firstName).

populate 函数的第四个参数是一个选项对象,它可以有一个排序对象,但是这样做:

The fourth parameters of the populate function is an option object which can have a sort object, but doing this:

.populate('contact', 'firstName lastName', null, { sort {'firstName': 1}})

不起作用(似乎对客户的联系人列表进行排序).

is not working (seem to sort contact list on customer).

我对猫鼬(和 mongo)完全陌生.我正在尝试将 Rails 项目移植到 node/express.

I am completely new to mongoose (and mongo). I am trying to port a rails projects to node/express.

有没有办法按 contact.firstName 对查询进行排序?

Is there a way I can sort my query by contact.firstName?

我最终手动进行排序 (Array.sort),但我真的不喜欢这个解决方案.Sort 是同步的,所以它会阻塞 node.js 主线程(如果我错了,请纠正我).

I ended up doing my sort manually (Array.sort) but I really don't like this solution. Sort is sync so it block node.js main thread (correct me if I'm wrong).

有什么我不明白的地方吗?排序数据集对我来说是一个数据库问题,而不是应用程序...我对将 Rails 应用程序转换为 node.js 抱有很大希望,但似乎某些标准操作(分页网格)真的很难实现!

Is there something I don't understand? Sorting dataset is for me a database concern and not application... I was getting lot of hope about turning my rails application into node.js but it seem that some standard operation (paging a grid) are really hard to implement!

推荐答案

您不能对虚拟字段或填充字段进行排序,因为这些字段仅存在于您的应用程序对象(Mongoose 模型实例)中,但排序是在 MongoDB 中执行的.

You can't sort on virtual fields or populated fields as those fields are only present in your app objects (Mongoose model instances) but the sort is executed within MongoDB.

这是 MongoDB 不支持连接所导致的主要限制之一.如果您的数据是高度相关的,那么您应该考虑使用关系数据库而不是 MongoDB.

This is one of the key limitations that results from MongoDB not supporting joins. If your data is highly relational then you should consider using a relational database instead of MongoDB.

这篇关于节点 - Mongoose 3.6 - 使用填充字段对查询进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆