In my question
document, I have a schema named QuestionSchema that includes fields such as title, body, user, category, comments, tags, image, and createdAt.
var QuestionSchema = new Schema({
title: {
type: String,
default: '',
trim: true
},
body: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
category: [],
comments: [{
body: {
type: String,
default: ''
},
root: {
type: String,
default: ''
},
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
createdAt: {
type: Date,
default: Date.now
}
}],
tags: {
type: [],
get: getTags,
set: setTags
},
image: {
cdnUri: String,
files: []
},
createdAt: {
type: Date,
default: Date.now
}
});
I am facing an issue where I need to sort the comments
array by the root field. I attempted to manually sort the array at the backend and also tried using aggregation but was unsuccessful in sorting it. Can someone please provide assistance?