Thursday, March 20, 2014

MongoDB 2.4 - Java (connecting to a replica set)

import java.net.UnknownHostException;
import java.util.Arrays;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException,
InterruptedException {
MongoClient client = new MongoClient(Arrays.asList(new ServerAddress(
"localhost", 27017), new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DBCollection test = client.getDB("database")
.getCollection("collection");

test.drop();

for (int i = 0; i < Integer.MAX_VALUE; i++) {
test.insert(new BasicDBObject("_id", i));
System.out.println("Inserted document: " + i);
Thread.sleep(500);
}
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.