connecting to mongodb
vimes1984 opened this issue · 20 comments
Can somebody please provide an example of how to connect to a mongodb running on the same machine?
I've tried connecting to the MongoDB via the Scritpcraft commandline and I'm failing miserably,....
help?
As a note for others looking for this information, samkass is correct. The best way is to add the library is in the commandline. Though, it took a fair bit of Googling to find exactly HOW to do that correctly. We now have MongoDB loaded and conversing with ScriptCraft objects. Basically its this: typically, when running a Spigot server, you exit something like java [java options] -jar spigot.jar [spigot options]
. However what you want to do to add java libraries for use in plugins within Spigot, start it like this instead:
java [other java options] -cp mongodb_driver.jar:mongodb_driver_core.jar:bson.jar:jongo.jar:spigot.jar org.bukkit.craftbukkit.Main nogui
This version of starting the server adds several other needed libraries to make it all work (which you will need to download and copy into the \plugins
folder). The last thing is that you will need a bit of javascript library suplementation to grant and ease the access to it. You can look at a project where we are working on in preparation for a pull-request at https://bitbucket.org/Growlf/tusail - Pay attention to the links for downloads to the other Java libraries - the one marked with version numbers seemed to matter.
Yes! Indeed - I am very interested in this answer as well. We have copied the Mongodb javascript library into the plugins/modules folder from https://github.com/mongodb/node-mongodb-native and attempted to access our running mongodb. It does not seem to function (and I don't think that is a surprize, really - simply copying the library without the dependencies was just random thrashing). Is there a way to use npm with scriptcraft to install libraries?
@growlf ScriptCraft doesn't run on Node.js, which npm runs on. Therefore, the libraries don't have access to Node.js's API, which provides all of the bindings to connections, etc etc...
To be able to connect to Mongo, you'd have to either write your own driver, or fork the node-mongodb-native library, and patch the native API that the library uses.
But Mongo has a Java driver for rhino which is what ScriptCraft runs on...
docs.mongodb.org/ecosystem/drivers/java/
On 12 August 2015 at 00:49, Иван К [email protected] wrote:
@growlf https://github.com/growlf ScriptCraft doesn't run on Node.js,
which npm runs on. Therefore, the libraries don't have access to Node.js's
API, which provides all of the bindings to connections, etc etc...To be able to connect to Mongo, you'd have to either write your own
driver, or fork the node-mongodb-native library, and patch the native API.—
Reply to this email directly or view it on GitHub
#263 (comment)
.
Oh yeah, I forgot about that. Was thinking in JS, my bad.
Yeah, you can implement the Java driver, but unless you can dynamically load it, you'll likely need to bake in and then compile the ScriptCraft JAR manually.
I have the Java driver installed... I need acesses to the mongo_client to
actually connect to the db from the JS console... Requiring the
mongoDB_client.js file does not work..
On 12 August 2015 at 00:52, Иван К [email protected] wrote:
Oh yeah, I forgot about that. Was thinking in JS, my bad.
Yeah, you can implement the Java driver, but unless you can dynamically
load it, you'll likely need to compile the JAR separately.—
Reply to this email directly or view it on GitHub
#263 (comment)
.
If the JAR is installed into ScriptCraft, and is loaded, then you should be able to access it via JS's top-level object Packages
.
e.g.
var MongoClient = Packages.com.mongodb.MongoClient;
var client = new MongoClient("localhost");
See the API:
http://api.mongodb.org/java/3.1/com/mongodb/MongoClient.html
Note: It's been a while since I wrote Javascript under Rhino/Nashorn, so I may be wrong, if they changed something or if my memory is failing me.
Grab the ScriptCraft source, change the source so when it builds the JAR, it builds it with the MongoDB driver library.
NEVER MIND, it's Ant. See https://github.com/walterhiggins/ScriptCraft/blob/master/contributing.md
To build, run "ant".
I've now run into this exact same error: #88
I added <pathelement path="${lib.mongodb}"/>
to the build.xml <classpath>
, added <property name="lib.mongodb" location="lib/mongodb-driver-3.0.3.jar"/>
and downloaded the mongo-java-driver-3.0.2.jar into the lib folder next to the bukkit jar and then ran ant. Copied the resultant scriptcraft.jar from the target dir into my server and restarted it.
How can I test that I have, in fact, gotten it right so far? Somehow this part seemed too simple.. and I suspect that I need to reference it more specifically somehow. Because the next bit is surely more convoluted - the bit about adding in the javascript libraries as well into the modules (??) dir, and I would like to know that tis first part is actually done correctly first.
(and THANK you, SEAPUNK for your help! on this :) )
I was thinking that (from the server console) something like this:
js var test=require('mongodb'); print(test.connect)
Would work, and would simply spew the code module to the screen. While that did not work at all (reported undefined) it also will not actually test the connection.
I have forked Walter's repo and begun working in it so that you can see what I have done so far: https://github.com/growlf/ScriptCraft
Providing I can get this working, it might be a nice resource for others. Possibly rework it into a pull-request for the original repo.
An alternate approach... If all you need is a jar on the classpath, such as the mongodb driver jar, you can add it at runtime when you start minecraft. I did this when fiddling with serial communication here: https://github.com/samkass/ScriptCraft-ContribSam/tree/master/serduino . Those instructions are probably a year out of date, but may be helpful in getting the jar loaded.
Somebody pointed me towards this which might help for mongo among many other things (like express, socket.io, and redis). Is it possible to use this or set this up with SC? If anybody has more time, it might be interesting to see if this would work.