well i have the following shemas:
var BrandSchema = new Schema({ name: { type: String, required: true, index: { unique: true }, lowercase: true }, logo: { type: ObjectId, ref: 'Image' } }); var FollowActionSchema = new Schema({ 'actionDate': { type: Date, 'default': Date.now }, 'brand': { type: ObjectId, ref: 'Brand' }, 'performer': { type: ObjectId, ref: 'User' }, 'type': String // followUser, folloBrand, followMerchant });
What i want is getting the user following brands, sorting by brand name, so for do that i did the query to FollowAction and find all the FollowActions that user did and then i populate the brand field.
So the problem is that i can't sort the query for the brand name, the only way i know to do that is by returning all documents and sort them from nodejs app. Anyone knows how can i do that?? or if i should change the shema structure??
The query that i do is:
async.waterfall([ function findActions(next) { var options = { query: { performer: userId, $or: [{ type: 'followBrand' }, { type: 'followMerchant' }] }, projection: { actionDate: 1, brand: 1, merchant: 1, type: 1 }, sort: '-actionDate', pagination: pagination }; utils.limitQuery('FollowAction', options, next); }, function inflate(actions, next) { total = actions.count; var options = { projection: { name: 1, _id: 1, username: 1 } }; async.eachSeries(actions.result, function(action, done) { async.waterfall([ function inflateAction(done) { action.inflate(options, done); }, function addToArray(item, done) { trusted.push({ _id: item._id, name: utils.capitalize(item.name || item.username), type: item.name ? 'brand' : 'merchant' }); return done(null, item); } ], done); }, next); } ], function(err) { callback(err, trusted, total); });