Skip to content

Instantly share code, notes, and snippets.

@john-morales
Created September 10, 2015 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-morales/4e2cde1bcc303ae2102b to your computer and use it in GitHub Desktop.
Save john-morales/4e2cde1bcc303ae2102b to your computer and use it in GitHub Desktop.
Java clusterMonitor repro
import com.mongodb.*;
import com.mongodb.util.JSON;
import com.mongodb.util.JSONSerializers;
import org.bson.BSON;
import org.bson.BSONObject;
import org.bson.types.ObjectId;
import org.json.JSONObject;
import org.junit.Test;
import java.io.*;
import java.net.Authenticator;
import java.net.Socket;
import java.util.*;
public class ClusterMonitorRepro {
public static void main(String[] args) throws Exception {
final MongoCredential credential = MongoCredential.createScramSha1Credential("usernameWithClusterMonitorRole", "admin", "password".toCharArray());
final MongoClient client = new MongoClient(new ServerAddress("localhost", 6000), Collections.singletonList(credential));
try {
final DBCollection collection = client.getDB("admin").getCollection("system.profile");
try (final DBCursor cursor = collection.find()
.sort(new BasicDBObject("$natural", -1))
.limit(20)
.setReadPreference(ReadPreference.primaryPreferred())) {
while(cursor.hasNext()) {
cursor.next();
}
}
} finally {
client.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment