ryanfitz / vogels

DynamoDB data mapper for node.js
Other
698 stars 125 forks source link

Get count of items in a StringSet attribute #181

Open simoami opened 8 years ago

simoami commented 8 years ago

I have a user.followers attribute of type stringSet(). Is it possible to get the count of entries instead of the entire list of follower entries, which would be inefficient if the list has thousands of items.

Can the follower count be returned along with other properties? say:

id: 123
fullName: "John Doe"
// followers: [...] <- excluded
// following: [...] <- excluded
// favorites: [...] <- excluded
stats: {
  followers: 10
  following: 5,
  favorites: 3
}
set-killer commented 8 years ago

Why not putting the followers in another table with hash/range key?

simoami commented 8 years ago

can you elaborate on your suggestion? will I be able to load the count from other tables along with the user's common attributes?

set-killer commented 8 years ago

oh, nvm.

You may use the projection attribute in the index (so when you search by that index you will allays get the desired values) like this example

Or you may use ProjectionExpression property when loading the model.

In both cases you should manually sync the stats with the corresponding data. However, skipping some fields from the return list will not save you any read capacities.

simoami commented 8 years ago

@set-killer I ended up creating a separate table for followers as you had suggested. I found that architecturally this can provide some flexibility. Also, I couldn't find a way to mix counts with other properties. So I chained calls to get the user profile and then made a call for each count property: followers, following...etc.

set-killer commented 8 years ago

I wonder how is this going to work in production environment with a lot of records in the database.

If this approach still consumes a lot of capacities you can mix the both approaches:

Btw, Instead of chaining the calls for the counts of followers/following/favorites you may perform the queries asynchronously with async.parallel. It would speed up the process as every table has its own capacities.