Node - Mongoose 3.6 - Sorting a request with a filled field

I am trying to execute a query used by a remote grid, so I have to handle sorting (asc, desc) for each field.

Here are the diagrams:

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); 

And here is the request:

 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); }; 

The query works when sorted by 'name' (as Customer.name) or 'address' (Customer.address), but cannot make it work when it is 'contact.firstName' (should be Customer.contact.firstName).

The fourth parameters for filling the fill is the option object, which can have a sort object, but at the same time:

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

does not work (it seems to sort the contact list at the client).

I'm brand new to mongoose (and mongo). I am trying to pass projets rails to node / express.

Is there a way to sort my request by contact.firstName?

Thanks!

Edit: I finished my sorting manually (Array.sort), but I really don't like this solution. Sorting is synchronization, so it blocks the main node.js thread (correct me if I am wrong).

Is there something I don’t understand? Sorting the dataset for me is related to the database, not the application ... I got a lot of hope that the rails application turned into node.js, but it looks like some standard operation (grid swapping) is really hard to implement!

+1
source share
1 answer

You cannot sort by virtual fields or fillable fields, since these fields are only present in your application objects (instances of the Mongoose model), but sorting is done in MongoDB.

This is one of the key limitations that MongoDB does not support connections. If your data is highly relational, you should consider using a relational database instead of MongoDB.

+8
source

Source: https://habr.com/ru/post/1233445/


All Articles