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
run:
system.indexes
system.users
BUILD SUCCESSFUL (total time: 1 second)
// 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();
// 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();
// 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());
}
// 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 );
public static void main(String[] args) {
}
@jasonmccay
jasonmccay / gist:1291684
Created October 17, 2011 01:06
Additional library references
import java.net.UnknownHostException;
import java.util.Set;
@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:1273352
Created October 9, 2011 05:32
Add Index method to the Controller
public ActionResult Index()
{
var model = new DeveloperModel();
var developers_collection = mongo_db.GetCollection("developers").FindAll().AsEnumerable();
model.developers = (from developer in developers_collection
select new DeveloperDTO
{
id = developer["_id"].AsObjectId,
name = developer["name"].AsString,
@jasonmccay
jasonmccay / gist:1273351
Created October 9, 2011 05:29
Creating a view for the Developer Listing.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<mongohq_csharp.Models.DeveloperModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Developers</h2>