MongoDB sorting by nested property

893 views
Skip to first unread message

Fábio Alves Domingues

unread,
Jun 22, 2014, 11:03:22 AM6/22/14
to sai...@googlegroups.com
Hello all,

I'm new in NodeJS and SailsJS. Sorry for my noob question.

I have the bellow two models:

Customer {
    attributes: {
        name: {
            type: 'string',
            required: 'true',
            maxLength: 255
        },
        gender: {
            type: 'string',
            maxLength: 1
        },
        status: {
            type: 'int',
            required: 'true'
        }
}

Appointment {
    attributes: {
        dateTimeStart: {
            type: 'datetime',
            required: 'true'
        },
        dateTimeEnd: {
            type: 'datetime',
            required: 'true'
        },
        description: {
            type: 'string',
            maxLength: 255
        },
        customer: {
            model: 'Customer',
            required: 'true'
        }
}

Everything works great...but now I want to query Appointments and sort by Customer name. I'm using the below code, but it's not working:

Appointment.find().populate('customer').sort('customer.name' : 1).exec(function(err, data) {...});

What's wrong? Someone can help me?

Regards,
Fábio Domingues

Mandeep Gulati

unread,
Jun 23, 2014, 7:41:15 AM6/23/14
to sai...@googlegroups.com
try this

sort("customer.name ASC")
Message has been deleted

Fábio Alves Domingues

unread,
Jun 23, 2014, 2:54:51 PM6/23/14
to sai...@googlegroups.com
Mandeep,

Unfortunately, it's not working.

It seems that sort just doesn't affect this field. In other, no related fields, sort works well!

Any idea?

Regards,
Fábio

Mandeep Gulati

unread,
Jun 24, 2014, 4:43:00 AM6/24/14
to sai...@googlegroups.com
Not sure, it works for me. Here is the query I use in my app

collectionToUse
            .find({'book': bookId})
            .sort("chapter.num ASC")
            .sort("seq ASC")
            .done(function (err, sections) {
              if (err) {
                callback(err, null)
              }
              else {
                callback(null, sections);
              }
            }
          );

On Sunday, June 22, 2014 8:33:22 PM UTC+5:30, Fábio Alves Domingues wrote:

Fábio Alves Domingues

unread,
Jun 25, 2014, 6:08:01 PM6/25/14
to sai...@googlegroups.com
Madeep,

I don't know if it's just your example code, but I see that you didn't use the populate methods. Can you please share your models and the complete find code? I'll check with your code if there is something wrong with mine.

Thanks for your attention.

Regards,
Fábio

Fábio Alves Domingues

unread,
Jun 25, 2014, 8:12:27 PM6/25/14
to sai...@googlegroups.com
I forgot to mention that I'm using MongoDB.

Fábio Alves Domingues

unread,
Jun 27, 2014, 6:41:33 PM6/27/14
to sai...@googlegroups.com
I'm still with the problem!

Anyone can help me?

Regards,
Fábio Domingues

Em domingo, 22 de junho de 2014 12h03min22s UTC-3, Fábio Alves Domingues escreveu:

Mandeep Gulati

unread,
Jul 2, 2014, 3:02:40 PM7/2/14
to sai...@googlegroups.com
Hi Fabio, Sorry  I wasn't available for the last few days to answer to your question. I am using 0.9 version of Sails and it does not support associations. The solution I gave you would have worked if the customer copy was embedded in Appointment instead of the customer reference. You can either wait for someone who has used this functionality in the beta version or you could sort the records in your application code after retrieval from the DB. It's not an efficient solution since you cannot leverage the index but if it's a limitation of waterline, you don't have another choice. 



On Sunday, June 22, 2014 8:33:22 PM UTC+5:30, Fábio Alves Domingues wrote:

Fábio Alves Domingues

unread,
Jul 5, 2014, 12:23:48 PM7/5/14
to sai...@googlegroups.com
Mandeep,

Thanks for your help!

Since we've just start the development, we'll wait for a solution using the associations.

If anyone has already worked with it, please share with me.

Regards,
Fábio Domingues

Em domingo, 22 de junho de 2014 12h03min22s UTC-3, Fábio Alves Domingues escreveu:

Dave Sag

unread,
Jul 6, 2014, 7:12:45 PM7/6/14
to sai...@googlegroups.com
Hi Mandeep,

I believe, especially if you are just starting on your project, you should use the 0.10.x release which includes associations.  I found the whole process of upgrading from 0.9 to 0.10 quite time consuming as I'd already baked in a number of things that then needed to be changed.

With regards to your sorting issue, I faced a similar issue, also with querying embedded content (One of my models includes an array of hashes - because simpler than creating a whole lot of associations in this particular case) and to query and sort on that content I ended up using lodash to find and sort from within those collections. This is acceptable to me because
a) it works (simplest thing that works)
b) there are only 20 records for this model (and it's static data loaded at seed time so never going to change), and
c) it's surprisingly fast, and a part of the system that's never going to be hammered too heavily.

I'm using Mongo as my back-end store and there are definitely restrictions on how much sorting etc you can do with it.  It's not an SQL database after all but a document store,


for some discussions of this.

Cheers

Dave

Fábio Alves Domingues

unread,
Jul 7, 2014, 8:30:23 AM7/7/14
to sai...@googlegroups.com
Hi Dave, how are you?
 
I'm using 0.10 version of Sails. But I still got problem with sorting by a field with an associated document.
 
I don't know if I'm doing something wrong, or, if it's a issue of Sails 0.10 and Waterline, or, maybe, if it's a mongo restriction.
 
Best regards,
Fábio Domingues
 
 
 
 
Em domingo, 22 de junho de 2014 12h03min22s UTC-3, Fábio Alves Domingues escreveu:

Dave Sag

unread,
Jul 7, 2014, 5:11:24 PM7/7/14
to sai...@googlegroups.com
It's a Mongo restriction.

Fábio Alves Domingues

unread,
Jul 9, 2014, 8:18:48 AM7/9/14
to sai...@googlegroups.com
Dave,
 
Thanks for your response.
 
We'll check what we will do with that.
 
Regards,
Fábio

Em domingo, 22 de junho de 2014 12h03min22s UTC-3, Fábio Alves Domingues escreveu:
Reply all
Reply to author
Forward
0 new messages