2017-06-19 38 views
0

我被困在一个点,我需要根据下面的日期时间字段中的文件进行排序是我的数据库结构排序阵列/子文件根据日期时间

"_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" : [ ] 
       } 
     ] 
} 

现在我想排序文档按照的降序排列,日期位于BlogPosts数组中。 MongoDB中控制台我想

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

但是在PHP我不能够做到这一点 我试图

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

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

我也试图在这个answer提到的解决方案但没有运气......请帮我出去

+0

使用相同的[ “点表示法”(https://docs.mongodb.com/manual/core/document/#点符号)形式,如在shell中。这部分不是PHP特定的:'$ cursor = $ collection-> find() - > sort(array(“BlogPosts.Date”=> -1));' –

+0

Thanku soo很多工作... –

回答

1

相反的:

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

尝试

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

Refer this question

+0

Thanku soo much那工作... –

+0

听起来不错! –