Skip to content

Instantly share code, notes, and snippets.

@fpigerre
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save fpigerre/40bcb873eae08ff5eb8b to your computer and use it in GitHub Desktop.

Select an option

Save fpigerre/40bcb873eae08ff5eb8b to your computer and use it in GitHub Desktop.
A simple MongoDB snippet detailing some common functions used when integrating MongoDB in a Java application.
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;
public class MongoDB {
// To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
// if it's a member of a replica set:
MongoClient mongoClient = new MongoClient();
// or
MongoClient mongoClient = new MongoClient( "localhost" );
// or
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DB db = mongoClient.getDB( "mydb" );
// Authentication
boolean auth = db.authenticate(myUserName, myPassword);
// Get a list of collections
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
// Get a specific collection
DBCollection coll = db.getCollection("testCollection");
// Insert a document in a collection
BasicDBObject doc = new BasicDBObject("name", "MongoDB").
append("type", "database").
append("count", 1).
append("info", new BasicDBObject("x", 203).append("y", 102));
coll.insert(doc);
// Find the first document in a collection
DBObject myDoc = coll.findOne();
// Get the amount of documents in a collection
System.out.println(coll.getCount());
// Iterate over a number of documents in a collection
// The find() method can be used to narrow down the selection of documents used
DBCursor cursor = coll.find();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
// Get a single document using a query
BasicDBObject query = new BasicDBObject("i", 71);
cursor = coll.find(query);
// Get a set of embedded documents
BasicDBObject query = new BasicDBObject("j", new BasicDBObject("$ne", 3)).
append("k", new BasicDBObject("$gt", 10));
cursor = coll.find(query);
// Get a set of documents
query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // e.g. find all where i > 50
cursor = coll.find(query);
// or
query = new BasicDBObject("i", new BasicDBObject("$gt", 20).
append("$lte", 30)); // i.e. 20 < i <= 30
cursor = coll.find(query);
// Create an index
coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending
// Get a list of indexes from a collection
List<DBObject> list = coll.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
// Get a list of databases
MongoClient mongoClient = new MongoClient();
for (String s : mongoClient.getDatabaseNames()) {
System.out.println(s);
}
// Drop a database
MongoClient mongoClient = new MongoClient();
mongoClient.dropDatabase("myDatabase");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment