2016-08-12 66 views
-1

內對文檔進行排序我有有一個樣本對象如下集合:的MongoDB:基於對象的數組

{ 
    name : 'Sachin Tendulkar', 
    followers : { 
     location : { 
      countries : [{ 
       name : 'India', 
       count : 12345 
      }, { 
       name : 'Pakistan', 
       count : 12345 
      },{ 
       name : 'Australia', 
       count : 12345 
      }], 
      cities : [{ 
       name : 'Mumbai', 
       count : 12345 
      },{ 
       name : 'Karachi', 
       count : 12345 
      },{ 
       name : 'Melborne', 
       count : 12345 
      }] 
      states : [{ 
       name : 'Maharastra', 
       count : 12345 
      },{ 
       name : 'Balochistan', 
       count : 12345 
      },{ 
       name : 'Sydney', 
       count : 12345 
      }] 
     } 
    } 
} 

我要立足本市計數的所有文件進行排序。例如,

Sort all documents according to a specific city i.e. Mumbai's count

你能幫助我建立排序文件按上述條件的查詢?

+2

請發表您的期望輸出...或者闡述更多 –

回答

0

我認爲這將是該查詢:

db.collection_name.aggregate([ 
{$unwind:"$followers.location.cities"}, 
{$match:{followers.location.cities.name:"Mumbai"}, 
{$sort:{"followers.location.cities.count":1}} 
])