0

I'm writing a library to abstract my data layer (it's going to use a mix of Mongo and Memcached). I've been testing Monk and can't figure out why the below script isn't finishing:

mongo = require("monk")("mongodb://#{options.mongodb.username}:#{options.mongodb.password}@#{options.mongodb.hostname}:#{options.mongodb.port}/#{options.mongodb.database}")
users = mongo.get("users")
find = users.findById 12345
find.complete (err, doc) ->
  console.dir doc
  console.dir err

It's returning the document to the log, { _id: 12345, foo: "bar" }, successfully but not completing when run using node test.js. Why is this?

Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57

1 Answers1

1

The reason the script stays alive is because the connection to MongoDB is still open. If you call mongo.close(); that should close the connection and provided you have nothing else keeping the event loop alive (e.g. network connections, timers, etc), then your script should terminate.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • That's fixed it, thanks. I did look into closing the connection but the method `close()` isn't stated anywhere in the [Monk docs](https://www.npmjs.org/package/monk). – Ryan Brodie Apr 26 '14 at 17:04
  • Yeah it's unfortunate, maybe submit a pull request to add it? – mscdex Apr 26 '14 at 17:25