Horn of Mongo
Currently tested against mongodb release: 2.4.6
All actions within the a mongodb-enabled Rhino
Context happen through
MongoRuntime. Before anything can happen, however, you need to first create a
MongoScope to work from using static methods within
MongoRuntime. After you have a
MongoScope you can execute just about any mongodb script you want from the convenient
There are two ways to create a
MongoScope. The first will create an environment without the
db global variable. The second will create a connected client for you but expects more information (ex. hostname(s) and a database name in the form of a
import org.github.nlloyd.hornofmongo.MongoRuntime; import org.github.nlloyd.hornofmongo.MongoScope; ... MongoScope myMongoScope = MongoRuntime.createMongoScope(); // then to create the 'db' global variable and open a connection to a mongod instance... MongoRuntime.call(new MongoScriptAction(myMongoScope, "connect", "db = connect('someHost:27017/someDb',myUserName,myPassword);")); // or if no authentication is required... MongoRuntime.call(new MongoScriptAction(myMongoScope, "connect", "db = connect('someHost:27017/someDb',null,null);")); ... MongoRuntime.call(new MongoScriptAction(myMongoScope, "your special script, or a Reader instead of this string"));
import org.github.nlloyd.hornofmongo.MongoRuntime; import org.github.nlloyd.hornofmongo.MongoScope; ... MongoScope myMongoScope = MongoRuntime.createMongoScope(new MongoClientURI("mongodb://localhost/test"), true, true); ... MongoRuntime.call(new MongoScriptAction(myMongoScope, "your special script, or a Reader instead of this string"));
After that you can feed scripts as strings or
Reader instances through the MongoRuntime.call(..) method wrapped in
Additional Configuration Options
MongoScope instance can be configured with two additional flags that can closely replicate the behavior observed in the official mongo shell client.
myMongoScope.setUseMongoShellWriteConcern(true) will configure the wrapped mongo client to use the WriteConcern used by the official mongo shell client which is send-and-forget.
myMongoScope.setMimicShellExceptionBehavior(true) will cause the Horn of Mongo environment to intercept some exception types from the wrapped mongo client and instead of throwing an exception in the mongodb-enabled Rhino environment will print an error to stdout. Which exceptions are intercepted have been determined based on the behavior of the official mongo shell client.
- run_program1.js is excluded due to the currently unsupported runProgram() function.
- mr_noscripting.js and logpath.js are excluded due to the currently unsupported MongoRunner js class.
- evalf.js is excluded due to a locking issue that has yet to be resolved, however the complexity of the test scenario makes this a safe-to-exclude for now.
- indexOtherNamespace.js excluded because I can't for the life of me get the failure scenario to actually fail using the mongo java driver :-(
- memory.js is excluded because it is testing the mongod rather than the client api behavior (and it takes a while to run on slower machines). This test does actually pass, however.
- remove_justone.js excluded until the mongo java driver supports that feature: https://jira.mongodb.org/browse/JAVA-759
- basicc.js, bench_test1.js
shellspawn.js, and updatef.js are excluded because they rely on the following functions
startParallelShell()which there are no plans to implement
Updated and tested against mongodb version 2.4.6 Upgraded to use mongo-java-driver 2.11.3 Applied patch from AquaFold that stores a reference to the DB instance after each API query for later getLastError() checks
Added CurrentDirectoryHandler interface as a mechanism similar to PrintHandler, QuitHandler, etc. for providing custom handling of cwd operations as well as path resolution. Additional minor changes provided by AquaFold applied.
Fixed minor classloader issue.
Updated and tested against mongodb version 2.4.5 Upgraded to use mongo-java-driver 2.11.2 fts_blogwild.js and fts_mix.js included in test suite, https://jira.mongodb.org/browse/JAVA-814 resolved in latest mongo-java-driver release Improved exception handling in several adaptor class constructors
Updated and tested against mongodb version 2.4.4
First release! Highly compatible release with mongodb version 2.4.3 (see Known Limitations).
Have a feature idea that you want to discuss? Bring it up here Have a question about a feature? Bring it up here Have a bug you want to report? Bring it up here Want to hear about the latest that is happening on this project? Read about it here!
Show your support by donating!
All of Horn of Mongo is licensed under the MIT license.
Copyright (c) 2012 Nick Lloyd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.