3

I'm trying to order the next model by ts (timestamp):

var Schema = new mongoose.Schema({
info: {
    name: { type: String, trim: true, validate: [
      { validator: validations.user.maxName, msg: 'The name must be shorter' }
    ]}
},
gender: { type: String, trim: true, enum: ['Male', 'Female'] },
  notifications: [{
    story: {
      _id: { type: mongoose.Schema.Types.ObjectId, ref: 'Story' },
      video_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Video' }
    },
    video: {
      _id: { type: mongoose.Schema.Types.ObjectId, ref: 'Video' },
      video_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Video' }
    },
    type: { type: String },
    read: { type: Number, default: 0 }, // 0 - Unread, 1 - read
    ts: { type: Date, default: Date.now }
  }]
}, { timestamps: { createdAt: 'created_at' } });

This is the code I'm trying to use in order to order these notification elements is what it follows:

UserSchema.statics.getNotifications = function (user_id) {
  return this.findById(user_id)
    .select('notifications')
    .populate({
      path: 'notifications.story.video_id',
      select: '_id user story',
      populate: ([{
        path: 'user',
        select: '_id nickname info.thumbnail'
      }, {
        path: 'story',
        select: 'title'
      }])
    })
    .populate({
      path: 'notifications.video.video_id',
      select: '_id user story parent',
      populate: ([{
        path: 'user',
        select: '_id nickname'
      }, {
        path: 'story',
        select: '_id'
      }])
    })
    .sort({ 'notifications.ts': -1 })
    .exec();
};

But instead of sorting my notifications, I guess I'm sorting the users that return my query with all the notifications.

Is there any way to sort for a given user, the notifications?

2
  • 2
    There isn't a way to sort array fields using find, it's typically better to keep the array sorted as it's updated using $push with the $sort modifier. Commented Dec 10, 2016 at 15:43
  • I will update my question with the final result. That really helped @JohnnyHK, thanks!. Commented Dec 10, 2016 at 16:32

2 Answers 2

3

Inside your populate you want to sort you can

.populate({ path: 'theoneyouwanttosort', options: { sort: { createdAt: -1 } } })

Hope that can help you :)

Sign up to request clarification or add additional context in comments.

1 Comment

This won't help, unfortunately, because the notifications field that the OP is trying to sort isn't the field being populated (it's various fields within the elements of that array that are being populated).
3

Thanks to @JohnnyHK here bellow I leave the answer of my question:

Schema.findByIdAndUpdate(user_id, {
  $push: {
    notifications: {
      "$each": [{
        story: {
          parent: mongoose.Types.ObjectId(video_bookmarked),
          video_id: mongoose.Types.ObjectId(video_id),
        },
        type: 'respond-video'
      }],
      "$sort": { ts: -1 }
    }
  },
  $inc: { count_notifications: 1 }
}).exec();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.