how to sort array of objects in mongodb [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • How to sort sub-documents in the array field?

    3 answers




My MongoDB document structure:



 _id:ObjectId("12345")
hai:Array
0:Object
designation:"software"
1:Object
designation:"hardware"
2:Object
designation:"Core"


I need to sort the designation in hai array.
I tried like this, but not working



db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)


Can anyone please help,
Thanks in advance










share|improve this question















marked as duplicate by chridam mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 8:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    0
















    This question already has an answer here:




    • How to sort sub-documents in the array field?

      3 answers




    My MongoDB document structure:



     _id:ObjectId("12345")
    hai:Array
    0:Object
    designation:"software"
    1:Object
    designation:"hardware"
    2:Object
    designation:"Core"


    I need to sort the designation in hai array.
    I tried like this, but not working



    db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
    db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)


    Can anyone please help,
    Thanks in advance










    share|improve this question















    marked as duplicate by chridam mongodb
    Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 4 at 8:16


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • How to sort sub-documents in the array field?

        3 answers




      My MongoDB document structure:



       _id:ObjectId("12345")
      hai:Array
      0:Object
      designation:"software"
      1:Object
      designation:"hardware"
      2:Object
      designation:"Core"


      I need to sort the designation in hai array.
      I tried like this, but not working



      db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
      db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)


      Can anyone please help,
      Thanks in advance










      share|improve this question

















      This question already has an answer here:




      • How to sort sub-documents in the array field?

        3 answers




      My MongoDB document structure:



       _id:ObjectId("12345")
      hai:Array
      0:Object
      designation:"software"
      1:Object
      designation:"hardware"
      2:Object
      designation:"Core"


      I need to sort the designation in hai array.
      I tried like this, but not working



      db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
      db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)


      Can anyone please help,
      Thanks in advance





      This question already has an answer here:




      • How to sort sub-documents in the array field?

        3 answers








      node.js mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 8:58









      GeekSambhu

      691819




      691819










      asked Jan 4 at 7:42









      PRUDHVI RAJPRUDHVI RAJ

      248




      248




      marked as duplicate by chridam mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 4 at 8:16


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by chridam mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 4 at 8:16


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          You have to unwind array first then you can able to sort data.



          ref link : Sort array






          share|improve this answer































            0














            The sort function expects an object like this (note the brackets in sort):



            db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})


            or



            db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})


            Also note if your sort is on a nested object then you need to use quotes around your key.



            Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.






            share|improve this answer
























            • I think the requirement is not sorting the documents, but sorting a field inside document.

              – Sreeragh A R
              Jan 4 at 8:10


















            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You have to unwind array first then you can able to sort data.



            ref link : Sort array






            share|improve this answer




























              0














              You have to unwind array first then you can able to sort data.



              ref link : Sort array






              share|improve this answer


























                0












                0








                0







                You have to unwind array first then you can able to sort data.



                ref link : Sort array






                share|improve this answer













                You have to unwind array first then you can able to sort data.



                ref link : Sort array







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 7:48









                Sachin ShahSachin Shah

                1,9241519




                1,9241519

























                    0














                    The sort function expects an object like this (note the brackets in sort):



                    db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})


                    or



                    db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})


                    Also note if your sort is on a nested object then you need to use quotes around your key.



                    Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.






                    share|improve this answer
























                    • I think the requirement is not sorting the documents, but sorting a field inside document.

                      – Sreeragh A R
                      Jan 4 at 8:10
















                    0














                    The sort function expects an object like this (note the brackets in sort):



                    db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})


                    or



                    db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})


                    Also note if your sort is on a nested object then you need to use quotes around your key.



                    Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.






                    share|improve this answer
























                    • I think the requirement is not sorting the documents, but sorting a field inside document.

                      – Sreeragh A R
                      Jan 4 at 8:10














                    0












                    0








                    0







                    The sort function expects an object like this (note the brackets in sort):



                    db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})


                    or



                    db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})


                    Also note if your sort is on a nested object then you need to use quotes around your key.



                    Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.






                    share|improve this answer













                    The sort function expects an object like this (note the brackets in sort):



                    db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})


                    or



                    db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})


                    Also note if your sort is on a nested object then you need to use quotes around your key.



                    Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 4 at 8:00









                    LongHikeLongHike

                    8121224




                    8121224













                    • I think the requirement is not sorting the documents, but sorting a field inside document.

                      – Sreeragh A R
                      Jan 4 at 8:10



















                    • I think the requirement is not sorting the documents, but sorting a field inside document.

                      – Sreeragh A R
                      Jan 4 at 8:10

















                    I think the requirement is not sorting the documents, but sorting a field inside document.

                    – Sreeragh A R
                    Jan 4 at 8:10





                    I think the requirement is not sorting the documents, but sorting a field inside document.

                    – Sreeragh A R
                    Jan 4 at 8:10



                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas