Anybody figured out a way to output to a console inside a javascript function executing server-side in Mongo? Groovy code below:
DBCollection js = db.getCollection('system.js')
js.save([
'_id' : 'product',
value : new Code("""
function(x,y) {
alert('product called!'); // blows up...
console.log('product called!'); // blows up...
return x*y;
}
"""
)
])
js.eval('product(2,3)')
There are two functions you can use to output from within JavaScript that is eval'd on the MongoDB server (including Map/Reduce functions):
print()
- standard javascript printprintjson()
- print as JSONBoth functions will emit their output into the mongod
log, and can be useful for debugging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With