2013-06-26 70 views
0

我從兩個單獨的mongodb模式向下面的對象填充數據: 他們是用戶和文章。我如何篩選/排序,以便我可以通過user._id找到骨幹文章? 這是針對單個頁面的博客類型網站,其中每個用戶都有自己的文章。如何在主幹中篩選此JSON?

我在看_.filter和_.where函數在下劃線中,但我對此仍然陌生。任何幫助表示讚賞。謝謝。

這裏是我的什麼即時試圖建立服務器上的一個例子(與嵌入式架構完成): http://kevg.co:3700/demo1 和骨幹模型/視圖/收集代碼是在這裏: http://kevg.co:3700/javascripts/demoj17.js

//Individual Model 
    { 
    "user": { 
     "username": "ho", 
     "email": "[email protected]", 
     "_id": "51be709a148846ec25000007" 
    }, 
    "name": "money", 
    "articlebody": "", 
    "_id": "51c1033283376a5808000002", 
    "__v": 0, 
    "createdAt": "2013-06-19T01:02:42.424Z" 
    }, 

//Rest of data 

    { 
    "user": { 
     "username": "kev", 
     "email": "[email protected]", 
     "_id": "51be6fe9148846ec25000001" 
    }, 
    "name": "bob", 
    "articlebody": "", 
    "_id": "51c89ab47596ef1018000001", 
    "__v": 0, 
    "createdAt": "2013-06-24T19:15:00.835Z" 
    }, 
    { 
    "user": { 
     "username": "kev", 
     "email": "[email protected]", 
     "_id": "51be6fe9148846ec25000001" 
    }, 
    "name": "sasa", 
    "articlebody": "sajdja", 
    "_id": "51c8a3bf341eb4141f000001", 
    "__v": 0, 
    "createdAt": "2013-06-24T19:53:35.233Z" 
    } 
+0

如果您可以發佈現有Backbone模型和集合定義的示例,這可能會有所幫助。 – damienc88

回答

2

我d有一組用戶以及一系列文章。

要得到一個特定的用戶已張貼的文章集合,你可以執行到一個類似於:

// theUser would be the user selected 
articlesCollection.where({'User': theUser}); 

感謝,Loamhoof您指出的減少。

請注意,您的用戶模型也可能包含用戶發佈的文章集合。

+1

相當於:'articlesCollection.where({'User':theUser});' – Loamhoof

+0

乾杯,一定是錯過了。 – damienc88

1

假設你的數據低於

var data = [{ 
    "user": { 
     "username": "ho", 
      "email": "[email protected]", 
      "_id": "51be709a148846ec25000007" 
    }, 
     "name": "money", 
     "articlebody": "", 
     "_id": "51c1033283376a5808000002", 
     "__v": 0, 
     "createdAt": "2013-06-19T01:02:42.424Z" 
}, { 
    "user": { 
     "username": "kev", 
      "email": "[email protected]", 
      "_id": "51be6fe9148846ec25000001" 
    }, 
     "name": "bob", 
     "articlebody": "", 
     "_id": "51c89ab47596ef1018000001", 
     "__v": 0, 
     "createdAt": "2013-06-24T19:15:00.835Z" 
}, { 
    "user": { 
     "username": "kev", 
      "email": "[email protected]", 
      "_id": "51be6fe9148846ec25000001" 
    }, 
     "name": "sasa", 
     "articlebody": "sajdja", 
     "_id": "51c8a3bf341eb4141f000001", 
     "__v": 0, 
     "createdAt": "2013-06-24T19:53:35.233Z" 
}]; 

給出你可以使用Array過濾功能或一個以下劃線來過濾數據

function getArticles(userName) { 
    return _.filter(data, function (item) { 
     return item.user.username == userName; // give the property to be used for filtering 
    }); 
} 
var kevArticles = getArticles("kev"); 
var hoArticles = getArticles("ho"); 

或者你可以使用下劃線一個groupBy到關於用戶分組數據

function groupByUserName(){ 
    return _.groupBy(data, function (item) { 
     return item.user.username; // give the property to be used for filtering 
    }); 
} 

var groupedData = groupByUserName(); 

這將使它在下面給出的格式中

{ 
    "ho": [{ 
     "user": { 
      "username": "ho", 
      "email": "[email protected]", 
      "_id": "51be709a148846ec25000007" 
     }, 
     "name": "money", 
     "articlebody": "", 
     "_id": "51c1033283376a5808000002", 
     "__v": 0, 
     "createdAt": "2013-06-19T01:02:42.424Z" 
    }], 
    "kev": [{ 
     "user": { 
      "username": "kev", 
      "email": "[email protected]", 
      "_id": "51be6fe9148846ec25000001" 
     }, 
     "name": "bob", 
     "articlebody": "", 
     "_id": "51c89ab47596ef1018000001", 
     "__v": 0, 
     "createdAt": "2013-06-24T19:15:00.835Z" 
    }, { 
     "user": { 
      "username": "kev", 
      "email": "[email protected]", 
      "_id": "51be6fe9148846ec25000001" 
     }, 
     "name": "sasa", 
     "articlebody": "sajdja", 
     "_id": "51c8a3bf341eb4141f000001", 
     "__v": 0, 
     "createdAt": "2013-06-24T19:53:35.233Z" 
    }]