0

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?

Community
  • 1
  • 1
B. Robinson
  • 106
  • 1
  • 6
  • Firstly, are you using the MongoDB Java driver directly? I.e. you're not using another library (like Spring, Morphia, or MongoJack) to talk to MongoDB? If you are using the MongoDB Java driver, then can you post the contents of the newMongoClient and connectDB methods, since these seem to be doing the actual work that you're having trouble with. Finally, you shouldn't need to have to execute Javascript on the server to do this, you can do it all in Java with separate MongoClients for each server to connect to. – Trisha Apr 29 '14 at 08:48
  • I am using mongo-java-driver-2.11.3 directly. I do want to execute the script on the server because the source (db2) and the sink (sever at db1) and my client are all on separate networks. I did solve it by doing it in Java. However, that's slower because the data has to travel from through the intermediate client, rather than directly between the two servers. – B. Robinson Apr 30 '14 at 14:52
  • 1
    The Java solution is here: http://stackoverflow.com/questions/16164413/mongodb-copy-collection-in-java-without-looping-all-items/22875605#22875605 – B. Robinson Apr 30 '14 at 14:54

0 Answers0