I think this can be compacted into one logical block, or at least have both blocks do the same query. It'd be safer to have new achievements also use upsert. There may be bugs, in which case adding a doc to the database each time might result in duplicate earned achievement objects.

Alternatively, we could have a unique index for user and achievement. That will help both in speeding up queries for all achievements for a given user and achievement, and will ensure there are never more than one earned achievement for each user/achievement pair. See plugins.coffee for how to create indexes through Mongoose. NamedPlugin has one to ensure there's never more than one document with a given slug.

I think I'd prefer the latter solution, but both would work.