/** * Executes a database command. * * @param cmd {@code DBObject} representation the command to be executed * @param options query options to use * @param readPrefs {@link ReadPreference} for this command (nodes selection is the biggest part * of this) * @param encoder {@link DBEncoder} to be used for command encoding * @return result of the command execution * @throws MongoException * @dochub commands */ public CommandResult command( DBObject cmd, int options, ReadPreference readPrefs, DBEncoder encoder) { readPrefs = getCommandReadPreference(cmd, readPrefs); cmd = wrapCommand(cmd, readPrefs); Iterator<DBObject> i = getCollection("$cmd") .__find( cmd, new BasicDBObject(), 0, -1, 0, options, readPrefs, DefaultDBDecoder.FACTORY.create(), encoder); if (i == null || !i.hasNext()) return null; DBObject res = i.next(); ServerAddress sa = (i instanceof Result) ? ((Result) i).getServerAddress() : null; CommandResult cr = new CommandResult(sa); cr.putAll(res); return cr; }
private CommandResult convertToCommandResult(DBObject cmd, Response res) { if (res.size() == 0) return null; if (res.size() > 1) throw new MongoInternalException("something is wrong. size:" + res.size()); DBObject data = res.get(0); if (data == null) throw new MongoInternalException("something is wrong, no command result"); CommandResult cr = new CommandResult(cmd, res.serverUsed()); cr.putAll(data); return cr; }