1

I am not able to sort the nested array. I have referred many links such as link 1 but still I haven't got proper solution for my problem.

Here is my JSON data:

{
  _id: "5a8bf7e3903cc83237ebd014",
  prospect_name: "Akruthi",
  phone: "9878979879",
  email: "",
  address: "Kundapur",
  landmark: "Grt",
  city: "Udupi",
  state: "5a869c795eb34ecae3ab1ef6",
  region: "5a8689725eb34ecae3ab1ede",
  methodology: 2,
  sub_category: "colour",
  created_by: "asdadasad",
  referred_by: "Self",
  remarks: [
          {
            data: "Good",
            user: "5a8c0642903cc83237ebd01f",
            created_at: "2018-02-20T10:26:43.686Z",
            remarksId: "5a8bf7e3903cc83237ebd013",
          },
          {
            data: "test 1",
            user: "5a858129e6e8f6724d6aa551",
            created_at: "2018-02-21T04:35:30.615Z",
            remarksId: "5a8cf7122ad6f00da98a3f05",
          },
          {
            data: "test 2",
            user: "5a858129e6e8f6724d6aa551",
            created_at: "2018-02-21T04:41:45.514Z",
            remarksId: "5a8cf889160046121e3f1b25",
          },
          {
            data: "test 3",
            user: "5a858129e6e8f6724d6aa551",
            created_at: "2018-02-21T04:54:01.018Z",
            remarksId: "5a8cfb69ea4e7c14c1f7c550",
          },
         ],
  created_at: "2018-02-20T10:26:43.686Z"
}

I have done the following query in node.js:

return collection_name
 .aggregate([
                { $match: { _id: objectid(id) } },
                { $lookup: { from: 'state', localField: 'state', foreignField: '_id', as: 'state_details' } },
                { $lookup: { from: 'region', localField: 'region', foreignField: '_id', as: 'region_details' } },
                { $lookup: { from: 'users', localField: 'remarks.user', foreignField: '_id', as: 'remarks_details' } },
                { $unwind: '$remarks' },
                { $sort: { 'remarks.created_at': -1 } },
                { $group: { _id: '$_id', remarks: { $push: '$remarks' } } },
            ])

and I got result as: (got the sorted result, but lost other fields)

{
 _id: "5a8bf7e3903cc83237ebd014",
 remarks: [
        {
          data: "test 3",
          user: "5a858129e6e8f6724d6aa551",
          created_at: "2018-02-21T04:54:01.018Z",
          remarksId: "5a8cfb69ea4e7c14c1f7c550"
        },
        {
          data: "test 2",
          user: "5a858129e6e8f6724d6aa551",
          created_at: "2018-02-21T04:41:45.514Z",
          remarksId: "5a8cf889160046121e3f1b25"
        },
        {
          data: "test 1",
          user: "5a858129e6e8f6724d6aa551",
          created_at: "2018-02-21T04:35:30.615Z",
          remarksId: "5a8cf7122ad6f00da98a3f05"
        },
        {
          data: "Good",
          user: "5a8c0642903cc83237ebd01f",
          created_at: "2018-02-20T10:26:43.686Z",
          remarksId: "5a8bf7e3903cc83237ebd013"
        }
         ]
}

In my result I lost all the other fields except remarks. I wanted the result along with other fields as well.. I tried $project operation but still it did not work.

expected result:

    {
  _id: "5a8bf7e3903cc83237ebd014",
  prospect_name: "Akruthi",
  phone: "9878979879",
  email: "",
  address: "Kundapur",
  landmark: "Grt",
  city: "Udupi",
  state: "5a869c795eb34ecae3ab1ef6",
  region: "5a8689725eb34ecae3ab1ede",
  methodology: 2,
  sub_category: "colour",
  created_by: "asdadasad",
  referred_by: "Self",
  remarks: [
          {
          data: "test 3",
          user: "5a858129e6e8f6724d6aa551",
          created_at: "2018-02-21T04:54:01.018Z",
          remarksId: "5a8cfb69ea4e7c14c1f7c550"
        },
        {
          data: "test 2",
          user: "5a858129e6e8f6724d6aa551",
          created_at: "2018-02-21T04:41:45.514Z",
          remarksId: "5a8cf889160046121e3f1b25"
        },
        {
          data: "test 1",
          user: "5a858129e6e8f6724d6aa551",
          created_at: "2018-02-21T04:35:30.615Z",
          remarksId: "5a8cf7122ad6f00da98a3f05"
        },
        {
          data: "Good",
          user: "5a8c0642903cc83237ebd01f",
          created_at: "2018-02-20T10:26:43.686Z",
          remarksId: "5a8bf7e3903cc83237ebd013"
        }
         ],
  created_at: "2018-02-20T10:26:43.686Z"
}
7
  • try $$root to get all fields. Commented Feb 21, 2018 at 8:45
  • can u give example of $root Commented Feb 21, 2018 at 8:46
  • I have updated comment. plz check Commented Feb 21, 2018 at 8:47
  • yeah can you give me $$root example with my query please. I have never tried $$root before Commented Feb 21, 2018 at 8:49
  • refer this stackoverflow.com/questions/23011193/… Commented Feb 21, 2018 at 8:50

1 Answer 1

0

Add
{ $project:{ prospect_name: 1, phone: 1, email: 1, address: 1, landmark: 1, city: 1, state: 1, region: 1, methodology: 1, sub_category: 1, created_by: 1, referred_by: 1 } } and replace your group by query with following line { $group: { _id: null, remarks: { $push: '$remarks' } } }

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

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.