java - doEval() With Multiline String Argument -
using groovy 2.3.6, gmongo 1.2, java 1.8, mongodb 3.0.0...
i'm trying use doeval()
count of documents in collection. (for various reasons want use doeval()
rather count()
). invoking doeval()
single line string argument returns retval double value of 1.0 , ok double value of 1.0 expect:
def str = "db.configurations.count({name: 'mike'})" database.doeval(str)
if argument on multiple lines retval returned null (ok 1.0).
def str = "db.configurations.count({\nname: 'mike'\n})" database.doeval(str)
i expect doeval return retval of 1.0 not null, in first example. bug or expectation incorrect? should doeval()
support multiline string argument?
from doeval()
javadoc:
evaluates javascript functions on database server. useful if need touch lot of data lightly, in case network transfer bottleneck.
parameters:
code
- string representation of javascript function
args
- arguments pass javascript function
so not passing javascript function doeval
method. anyway, result getting consistent result can invoking directly mongo shell:
> db.eval("db.configurations.count({name: 'mike'})"); 1 > db.eval("db.configurations.count({\nname: 'mike'\n})"); null
(i did not dig deeper on mongo shell javascript internals... :-)
passing javascript function can result want:
println db.configurations.count([name: 'mike']) def str = '''function(name) { return db.configurations.count({ name: name }); }''' println db.doeval(str, 'mike')
the above yields:
1 [serverused:/127.0.0.1:27017, retval:1.0, ok:1.0]
Comments
Post a Comment