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

查看:46
本文介绍了节点-猫鼬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 cant get it to work when it is 'contact.firstName' (should be Customer.contact.firstName).

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

The fourth parameters of the populate fonction is an option object wich 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 Projet移植到node/express.

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

有没有一种方法可以按contact.firstName对查询进行排序?

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

谢谢!

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

I ended up doing my sort manually (Array.sort) but i really dont 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.

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

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