Loading ...
Sorry, an error occurred while loading the content.

Referencing a second collection?

Expand Messages
  • Paul Lynch
    Is there a way in taffydb to reference an attribute of an item in a second collection when doing a find ? To extend the friends example a bit, suppose that
    Message 1 of 2 , May 7, 2009
    • 0 Attachment
      Is there a way in taffydb to reference an attribute of an item in a second collection when doing a "find"?

      To extend the "friends" example a bit, suppose that you also had a collection called "states", and you wanted to associate each "friend" with a "state".  Items in the states collection might have "abbreviation", "population", and "region" attributes.  Could one set things up so that you could run a find command to get all of your friends who live in a state where region = "NE"?

      A more difficult (but parallel) example would be to find your friend that lives in the state with the biggest population (among the states where you have friends, not across all states).

      I think both of these things could be accomplished by copying the state attributes into the friend collection records, but I am hoping there is a way to avoid duplicating that data.

      Thanks,
          --Paul



    • tacoman_cool
      Hi Paul, Ahh, you can do something like this. If you structure your data like this: var friendsbystate =
      Message 2 of 2 , May 7, 2009
      • 0 Attachment
        Hi Paul,

        Ahh, you can do something like this. If you structure your data like this:

        var friendsbystate = TAFFY([{state:"WA",population:100000,friends:TAFFY([{name:"Ian",gender:"M"},
        // ... more friends in Washington
        ])}
        // ... more states
        ]);

        Then you could do something like this to find your male friends in Washington:

        friendsbystate.find({state:"WA",friends:{has:{gender:"M"}}});

        if you use the "has" verbiage and you are working on a nested Taffy Collection it will pass that lookup on to the Taffy Collection itself.

        This isn't quite a join, but if your primary thing of interest is State then this makes sense. Hope it helps,

        Ian


        --- In taffydb@yahoogroups.com, Paul Lynch <plynchnlm@...> wrote:
        >
        > Is there a way in taffydb to reference an attribute of an item in a second
        > collection when doing a "find"?
        >
        > To extend the "friends" example a bit, suppose that you also had a
        > collection called "states", and you wanted to associate each "friend" with a
        > "state". Items in the states collection might have "abbreviation",
        > "population", and "region" attributes. Could one set things up so that you
        > could run a find command to get all of your friends who live in a state
        > where region = "NE"?
        >
        > A more difficult (but parallel) example would be to find your friend that
        > lives in the state with the biggest population (among the states where you
        > have friends, not across all states).
        >
        > I think both of these things could be accomplished by copying the state
        > attributes into the friend collection records, but I am hoping there is a
        > way to avoid duplicating that data.
        >
        > Thanks,
        > --Paul
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.