This question is like MongoDB - copy collection in java without looping all items However, it's not clear that the question has been resolved using a Java MongoDB driver.
I want to copy part of a collection from a remote mongodb to the local mongodb. The remote uses ssh and is addressed as port 27018.
Running this in the mongodb shell works:
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.9
connecting to: test
> var db2 = connect("localhost:27018/github");
connecting to: localhost:27018/github
> var db1 = connect("localhost:27017/github");
connecting to: localhost:27017/github
However, the same in Java produces this error:
Error: local Mongo constructor takes no args at src/mongo/shell/mongo.js:147
Java code snippet:
logger.debug("Starting mongo shell execution.");
final MongoClient Client = newMongoClient(host.getStringValue(), port.getIntValue());
final DB db = connectDB(Client, dB.getStringValue(), false);
logger.debug("Mongo execution script: " + script.getStringValue());
logger.info("Mongo execution result: " + db.eval(script.getStringValue()));
Execute script with eval:
var db2 = connect("localhost:27018/github");
db2.getMongo().setSlaveOk();
var db1 = connect("localhost:27017/github");
The same error also occurs if you run it as a script. I also ran it as a server script, based on this: http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/
Save the script on then server, and then execute:
db.system.js.save(
{ _id: "KScript",
value: function() {
var db2 = connect("localhost:27018/github");
db2.getMongo().setSlaveOk();
var db1 = connect("localhost:27017/github");
}
}
)
db.eval("KScript()")
db.system.js.remove( { "_id" : "KScript" } );
In the Java based eval, or the server script, the error is the same: Error: local Mongo constructor takes no args at src/mongo/shell/mongo.js:147
However, in the mongodb shell, it works.
Any ideas?