Skip to content

Instantly share code, notes, and snippets.

View jasonmccay's full-sized avatar
🏠
Working from home

Jason McCay jasonmccay

🏠
Working from home
View GitHub Profile
@jasonmccay
jasonmccay / gist:1291682
Created October 17, 2011 01:05
Add references to MongoDB library for Java
import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
@jasonmccay
jasonmccay / gist:1291684
Created October 17, 2011 01:06
Additional library references
import java.net.UnknownHostException;
import java.util.Set;
public static void main(String[] args) {
}
// Inspecting what the Mongo library makes available, we see that we will
// need the following:
// A Mongo class that takes the server name and the port.
// A DB class that takes the name of the database
// Declare a boolean variable that will hold the authentication status of the authenticate method on the db object.
public static void main(String[] args) {
Mongo m = new Mongo( "hatch.mongohq.com" , 10004 );
// One final step ... for the main method, we need to provide a way for it to
// throw an error if we cannot connect to the MongoHQ database.
public static void main(String[] args) throws UnknownHostException {
Mongo m = new Mongo( "hatch.mongohq.com" , 10004 );
DB db = m.getDB("my_database_name");
boolean auth = db.authenticate("cloudbees", "my_database_password".toCharArray());
}
// Here, we are adding a string named "my_collections" that will hold the names of the collection.
public static void main(String[] args) throws UnknownHostException {
Mongo m = new Mongo( "hatch.mongohq.com" , 10004 );
DB db = m.getDB("my_database_name");
boolean auth = db.authenticate("cloudbees", "my_database_password".toCharArray());
Set<String> my_collections = db.getCollectionNames();
// Finally, let's do a basic iteration and display the names of the collections in the NetBeans console.
public static void main(String[] args) throws UnknownHostException {
Mongo m = new Mongo( "hatch.mongohq.com" , 10004 );
DB db = m.getDB("my_database_name");
boolean auth = db.authenticate("cloudbees", "my_database_password".toCharArray());
Set<String> my_collections = db.getCollectionNames();
run:
system.indexes
system.users
BUILD SUCCESSFUL (total time: 1 second)
@jasonmccay
jasonmccay / gist:1341607
Created November 5, 2011 14:49
CSV file for MongoImport Example in MongoHQ
## CSV File Example for MongoImport into a MongoHQ database
First Name,Last Name,Age,Position,Experience
Cameron,Newton,22,QB,Rookie
Darren,McFadden,24,RB,4yr
Mike,Pouncey,22,C,Rookie
@jasonmccay
jasonmccay / gist:1341650
Created November 5, 2011 15:18
MongoImport Command used to Import Data into a MongoHQ Database
## Example of MongoImport Used to Import Data into a MongoHQ Database ##
mongoimport -h staff.mongohq.com --port 10097 -d import_example -c football_players -u mongohq -p****** --type csv --file ~/Desktop/football_players.csv --headerline