Skip to content

Instantly share code, notes, and snippets.

@alexspurling
Created June 28, 2010 18:22
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 alexspurling/456180 to your computer and use it in GitHub Desktop.
Save alexspurling/456180 to your computer and use it in GitHub Desktop.
Simple example of MongoDB driver for Java
public class Comment {
private final String author;
private final String body;
public Comment(String author, String body) {
this.author = author;
this.body = body;
}
public String getAuthor() {
return author;
}
public String getBody() {
return body;
}
}
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class MongoPostTest {
public static void main(String[] args) throws UnknownHostException, MongoException {
// Initialise the connection to MongoDB
Mongo m = new Mongo("localhost", 27017);
DB db = m.getDB("mydb");
// Create a post and some comments to insert
List<Comment> comments = new ArrayList<Comment>();
comments.add(new Comment("bob", "comment 1"));
comments.add(new Comment("alfred", "comment 2"));
Post post = new Post(new Date(), "New blog post", "Welcome to my blog", comments);
// Get the posts collection and store the post
PostDAO postDAO = new PostDAO();
postDAO.setDataSource(db);
postDAO.storePost(post);
// Get all posts and print them
DBCollection postsColl = db.getCollection("posts");
DBCursor cur = postsColl.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
}
}
import java.util.Date;
import java.util.List;
public class Post {
private final Date date;
private final String title;
private final String body;
private final List<Comment> comments;
public Post(Date date, String title, String body, List<Comment> comments) {
this.date = date;
this.title = title;
this.body = body;
this.comments = comments;
}
public Date getDate() {
return date;
}
public String getTitle() {
return title;
}
public String getBody() {
return body;
}
public List<Comment> getComments() {
return comments;
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
public class PostDAO {
private DB db;
public void setDataSource(DB db) {
this.db = db;
}
public void storePost(Post post) {
DBCollection postsColl = db.getCollection("posts");
postsColl.insert(getPostObj(post));
}
public DBObject getPostObj(Post post) {
Map<String, Object> postMap = new HashMap<String, Object>();
postMap.put("date", post.getDate());
postMap.put("title", post.getTitle());
postMap.put("body", post.getBody());
List<DBObject> comments = new ArrayList<DBObject>();
for (Comment comment : post.getComments()) {
comments.add(getCommentObj(comment));
}
postMap.put("comments", comments);
return new BasicDBObject(postMap);
}
private DBObject getCommentObj(Comment comment) {
Map<String, Object> commentMap = new HashMap<String, Object>();
commentMap.put("author", comment.getAuthor());
commentMap.put("body", comment.getBody());
return new BasicDBObject(commentMap);
}
public List<Post> getAllPosts(long id) {
DBCollection postsColl = db.getCollection("posts");
DBCursor cur = postsColl.find();
List<Post> posts = new ArrayList<Post>();
while(cur.hasNext()) {
posts.add(getPost(cur.next()));
}
return posts;
}
private Post getPost(DBObject postObj) {
Date date = (Date) postObj.get("date");
String title = (String) postObj.get("title");
String body = (String) postObj.get("body");
List<Comment> comments = new ArrayList<Comment>();
for (Object commentObj : (BasicDBList) postObj.get("comments")) {
comments.add(getComment((DBObject) commentObj));
}
return new Post(date, title, body, comments);
}
private Comment getComment(DBObject commentObj) {
String author = (String) commentObj.get("author");
String body = (String) commentObj.get("body");
return new Comment(author, body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment