2017-06-19 4 views
0

ich an einem Punkt stecken bin, wo ich die Dokumente sortiert werden müssen, basierend auf dem Datum Zeitfeld finden Sie meine DatenbankstrukturSortierung Array/sub Dokument in mongodb PHP basierend auf Datetime

"_id" : ObjectId("59199bbb05b505777d86e9a2"), 
    "Email" : "[email protected]", 
    "BlogPosts" : [ 
      { 
        "PostID" : 7, 
        "Title" : "Party Time", 
        "Name" : "Sagar Khan", 
        "Description" : "Farewell party with 2k17 batchmates at atmosphere 4... #AllNight #Fun #CHEERS....", 
        "Date" : ISODate("2017-05-15T13:18:01Z"), 
        "Image" : "users/[email protected]/pictures/prof-pic.jpg", 
        "Likes" : [ 
          "[email protected]", 
          "[email protected]", 
          "[email protected]", 
          "[email protected]" 
        ], 
        "Comments" : [ 
          { 
            "CommentID" : 4, 
            "email" : "[email protected]", 
            "Name" : "Harish Shinde", 
            "Image" : "users/[email protected]/pictures/prof-pic.jpg", 
            "comment" : "Yo... Cheers ", 
            "Time" : ISODate("2017-05-15T13:18:40Z") 
          } 
        ] 
      } 
    ]} 

{ 
     "_id" : ObjectId("59450ce02aa01e3027df57be"), 
     "Email" : "[email protected]", 
     "BlogPosts" : [ 
       { 
         "PostID" : 2, 
         "Title" : "At Goa", 
         "Name" : "Harish Shinde", 
         "Description" : "Relaxing at baga Beach Goa ", 
         "Date" : ISODate("2017-06-17T11:05:20Z"), 
         "Image" : "users/[email protected]/pictures/prof-pic.jpg", 
         "Likes" : [ 
           "[email protected]" 
         ], 
         "Comments" : [ ] 
       } 
     ] 
} 

Jetzt will ich sortieren Die Dokumente in absteigender Reihenfolge basierend auf Datum im BlogPosts-Array. In MongoDB-Konsole habe ich versucht

db.Timeline.find().sort({BlogPosts.Date : -1 }).pretty() 

Aber In PHP bin ich die ich

$cursor = $collection->find()->sort(array("BlogPosts"=>array("Date"=> -1))); 

versucht zu tun, nicht in der Lage und

$cursor = $collection->find()->sort(array("BlogPosts.$.Date"=> -1)); 

ich auch versucht, die in diesen answer genannte Lösung aber kein Glück ... Bitte helfen Sie mir

+0

Verwenden Sie die gleiche [ "Punktnotation"] (https://docs.mongodb.com/manual/core/document/# Punkt-Notation) Form wie in der Shell. Dieser Teil ist nicht PHP-spezifisch: '$ cursor = $ collection-> find() -> sort (array (" BlogPosts.Date "=> -1));' –

+0

Vielen Dank, so viel, dass ... –

Antwort

1

Statt:

sort(array("BlogPosts.$.Date"=> -1)); 

versuchen

sort(array("BlogPosts.Date"=> -1)); 

Refer this question

+0

Thanku soo viel das hat funktioniert ... –

+0

Klingt gut !! –

Verwandte Themen