1ManStartup
1ManStartup

Reputation: 3816

How Can I Filter this JSON in Backbone?

I populated data from two separate mongodb schemas into an object below: They are user and article. How can I filter/sort so I can find articles by user._id in backbone? This is for a single page blog type site in which each user has their own articles.

I was looking at _.filter and _.where functions in underscore, but am still new to this. Any help is appreciated. Thanks.

Here is an example on my server of what im trying to build (was done with embedded schema): http://kevg.co:3700/demo1 and backbone model/view/collection code is here: 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"
  }

Upvotes: 0

Views: 157

Answers (2)

damienc88
damienc88

Reputation: 1977

I'd have a collection of users as well as a collection of articles.

To get the collection of articles a specific user has posted, you could perform something akin to:

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

Thanks, Loamhoof for pointing out the reduction.

Note, your User model could also hold a collection of Articles the user has posted.

Upvotes: 2

Diode
Diode

Reputation: 25155

Assuming your data as given below

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"
}];

you can use the filter function in Array or the one in underscore to filter the data

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");

Or you can use a groupBy in underscore to group the data with respect to the user

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

 var groupedData = groupByUserName();

This will make it in the format given below

{
    "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"
    }]

Upvotes: 1

Related Questions