Depanker Sharma
Depanker Sharma

Reputation: 72

Updating to mongo 3.0

I was working on mongo 2.X and have now updated to Mongo 3.X also updated java mongodb client to 3.0.3, I have js on mongo server which needs to be executed and js requires two object parameters This was easily done in previous version like this:

DB database = mongoclient.getDb(db);
CommandResult cr = database.doEval(js, query, collection); // js is String, query is BasicDBObject and collection is List<String>

Need help in running the same but now in place of DB I am getting MongoDatabase (as getDB has been deprecated) and doEval is replaced with runCommand

Upvotes: 0

Views: 256

Answers (1)

Trisha
Trisha

Reputation: 3931

You can actually look in the code of the original doEval method to figure out how to do this, it looks something like:

public CommandResult doEval(final String code, final Object... args) {
    DBObject commandDocument = new BasicDBObject("$eval", code).append("args", Arrays.asList(args));
    return executeCommand(wrap(commandDocument));
}

That means you'll need to do something like:

Document commandDocument = new Document("$eval", code).append("args", Arrays.asList(args));
return runCommand(commandDocument);

Note that running scripts on the server from application code is not really recommended, you're better off translating what it does into Java code, as the driver has much more efficient support for this.

Upvotes: 2

Related Questions