skip to Main Content

How can i get sorted data in array by "name" in categories where ‘_id = 63fe948a22009cbbba53b905’?

My sample data:

{
    "_id": "63fe948a22009cbbba53b905",
    "options": {
        "sort": "manual"
    },
    "categories": [
        {
            "priority": 0,
            "options": "manual",
            "name": "Nazwa 1",
            "_id": "63fe8cc76d50062ae1f53821"
        },
        {
            "priority": 0,
            "options": "manual",
            "name": "Inna-Nazwa",
            "_id": "63fe8cc76d50062ae1f53822"
        },
        {
            "priority": 0,
            "options": "manual",
            "name": "Nowa",
            "_id": "63fea35cbbbaf3053ad1b25f"
        },
        {
            "priority": 0,
            "options": "manual",
            "name": "Ihahaowa",
            "_id": "63fea38118acd0f4d9ad4cac"
        }
    ],
    "updatedAt": "2023-03-01T01:10:49.817Z"
}

Result (sort data):

{
    "options": {
        "sort": "manual"
    },
    "categories": [
        {
            "name": "Ihahaowa",
            "_id": "63fea38118acd0f4d9ad4cac"
        },
        {
            "name": "Inna-Nazwa",
            "_id": "63fe8cc76d50062ae1f53822"
        },
        {
            "name": "Nazwa 1",
            "_id": "63fe8cc76d50062ae1f53821"
        },
        {
            "name": "Nowa",
            "_id": "63fea35cbbbaf3053ad1b25f"
        }
    ]
}

Sorry, but this is my beginnings with MongoDB. I has tried but it doesn’t work:

await NotesModel.findOne({ _id: id }, { 'options': 1, 'categories.name': 1, 'categories._id': 1, '_id': 0 }, {'categories.name': 1})

Thanks in advance for your help.

2

Answers


  1. Sorting an array within a document is not supported in a projection, so you won’t be able to use findOne for that.

    You could use aggregation with 3 stages:

    • $match to filter by _id
    • $project to select the fields you want returned
    • $addFields with $sortArray to order the categories
    Login or Signup to reply.
  2. for version 5.2 above

    db.data.aggregate([
      {
        "$match": {
          _id: "63fe948a22009cbbba53b905"
        }
      },
      {
        $addFields: {
          result: {
            $sortArray: {
              input: "$categories",
              sortBy: {
                name: 1 //// type sort 1 | -1 => ASC | DESC
              }
            }
          }
        }
      },
      {
        "$unwind": "$result"
      },
      {
        $group: {
          _id: "$_id",
          categories: {
            $push: {
              name: "$result.name",
              priority: "$result.priority",
              
            }
          }
        }
      },
      {
        "$unset": "result"
      },
      
    ])
    

    https://mongoplayground.net/p/S3P4WtU8hTi

    for lower version

    db.data.aggregate([
      {
        "$match": {
          _id: "63fe948a22009cbbba53b905"
        }
      },
      {
        "$unwind": "$categories"
      },
      {
        $sort: {
          "categories.name": 1
        }
      },
      {
        $group: {
          _id: "$_id",
          categories: {
            $push: {
              name: "$categories.name",
              priority: "$categories.priority",
              
            }
          }
        }
      },
      
    ])
    

    try it on https://mongoplayground.net/p/S3ZAXEcHdku

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search