can you post a ticket with some code on github. I'll have a look during the week.
One big observation that will save you a lot of pain and suffering. Don't execute javascript on the server as part of you business logic it will seriously hamper your scalability. The reasons are very simple.
1. The javascript engine is single threaded (spidermonkey)
2. Eval will take out a write lock for the entire duration of the execution limiting your ability to scale writes.
I know it's tempting but javascript on mongodb was never designed to be mongodb stored procedures. The only valid reason is if you are using them during f.ex map-reduce jobs.