Abdur Rehman
Abdur Rehman

Reputation: 75

Sort in nasted Object using mongo db Aggregate?

i have story data in which i want to order by desc inner object based on createdDate. y code is below

Story.aggregate(
[
  { 
    "$project" : { 
        "_id" : 0, 
        "stories" : "$$ROOT"
    }
}, 
{ 
    "$lookup" : { 
        "localField" : "stories.user", 
        "from" : "users", 
        "foreignField" : "_id", 
        "as" : "users"
    }
}, 
{ 
    "$unwind" : { 
        "path" : "$users", 
        "preserveNullAndEmptyArrays" : false
    }
}, 
{ 
    "$lookup" : { 
        "localField" : "stories.place", 
        "from" : "places", 
        "foreignField" : "_id", 
        "as" : "places"
    }
}, 
{ 
    "$unwind" : { 
        "path" : "$places", 
        "preserveNullAndEmptyArrays" : true
    }
},
{ 
    "$lookup" : { 
        "localField" : "places.categories", 
        "from" : "categories", 
        "foreignField" : "_id", 
        "as" : "categories"
    }
}, 
{ 
    "$unwind" : { 
        "path" : "$categories", 
        "preserveNullAndEmptyArrays" : false
    }
}, 
    { $match: { "stories._id": mongoose.Types.ObjectId(req.params.id) }},
    {
     "$sort":{"stories.comments.createdDate":-1}
    },
    { 
        "$project" : { 
            "stories._id" : "$stories._id", 
            "stories.title" : "$stories.title",
            "stories.createdDate" : "$stories.createdDate", 
            "stories.image" : "$stories.image", 
            "stories.likeCount" : "$stories.likeCount", 
            "stories.commentCount" : "$stories.commentCount", 
            "stories.likes" : "$stories.likes", 
            "stories.comments" : "$stories.comments",              
            "stories.story" : "$stories.story", 
            "users.firstName" : "$users.firstName",
            "users.storiesCount" : "$users.storiesCount", 
            "users.albumsCount" : "$users.albumsCount",
            "stories.urlTitle" : "$stories.urlTitle",
            "users._id" : "$users._id",
            "users.lastName" : "$users.lastName", 
            "users.image" : "$users.image", 
            "places.placeName" : "$places.placeName", 
            "places._id" : "$places._id", 
            "categories.categoryName" : "$categories.categoryName",
            "categories._id" : "$categories._id",                  
        }
      }
    ]
     

in this code i want to sort comment based on comments.created date.the data i recived from db is

 {
    "stories": {
        "_id": "5f86b6b4d11dfd67b083ce9c",
        "title": "Information about Khunjrab Pass.",
        "createdDate": "2020-10-14T09:31:00.407Z",
        "image": "Khunjerab-Pass.jpg",
        "likeCount": 0,
        "commentCount": 0,
        "likes": [
            "5f93d8571240934378d5ba06",
            "5f65ed4552e1d9156af84bd8"
        ],
        "comments": [
            {
                "_id": "5faf7e858de22b3a94bbc5a5",
                "userId": "5f93d8571240934378d5ba06",
                "comment": "some comment",                    
                "createdDate": "2020-11-14T06:51:49.269Z",
                "updatedDate": "2020-11-14T06:51:49.269Z",
                "likes": [
                    "5f9a53138dca473d58ae6e39"
                ],
            },
            {
                "_id": "5faf9a136525194c0077eddc",
                "userId": "5f65c4b7a9267211bfa994ff",
                "comment": "some comment",
               
                "likes": [
                    "5f9a53138dca473d58ae6e39"
                ],
            },
            {
                "_id": "5fafa5996525194c0077eddd",
                "userId": "5f65c4b7a9267211bfa994ff",
                "comment": "when we visited khunjrab top there was strong wind and very low temperature.",
                "firstName": "Sohaib",
                "lastName": "Siddique",
                "createdDate": "2020-11-14T09:38:33.789Z",
                "updatedDate": "2020-11-14T09:38:33.789Z",
                "likes": [
                    "5f9a53138dca473d58ae6e39"
                ],
                "image": "sohaib-img.jpeg"
            }
        ],
        "story": "Khunjerab Pass is a high mountain pass in the Karakoram Mountains, in a strategic position on the northern border of Pakistan and on the southwest border of China. Its elevation is 4,693 metres",
        "urlTitle": "prince-saiful-malook-and-badri-jamala"
    },
    "users": {
        "firstName": "Sohaib",
        "storiesCount": 1,
        "albumsCount": 0,
        "_id": "5f65c4b7a9267211bfa994ff",
        "lastName": "Siddique",
        "image": "sohaib-img.jpeg"
    },
    "places": {
        "placeName": "Khunjerab Pass",
        "_id": "5f65e66752e1d9156af84bd4"
    },
    "categories": {
        "categoryName": "Passes",
        "_id": "5f65cf7452e1d9156af84bcb"
    }
}

here i this story i got two comments on story i want to sort comment on the base of comments.createdDate but i don't get the desire result any solution?

thanks in advance

Upvotes: 0

Views: 34

Answers (1)

charlycou
charlycou

Reputation: 1988

Base on your document you can apply the following aggregation:

db.collection.aggregate([
  {
    "$unwind": "$stories.comments"
  },
  {
    "$sort": {
      "stories.comments.createdDate": -1
    }
  },
  {
    "$group": {
      "_id": "_id",
      "comments": {
        "$push": "$stories.comments"
      },
      "stories": {
        "$first": "$stories"
      },
      "users": {
        "$first": "$users"
      },
      "places": {
        "$first": "$places"
      },
      "categories": {
        "$first": "$categories"
      }
    }
  },
  {
    "$set": {
      "stories.comments": "$comments"
    }
  },
  {
    "$project": {
      "comments": 0
    }
  }
])

Upvotes: 1

Related Questions