Skip to content

Instantly share code, notes, and snippets.

@JaiHirsch
Created March 29, 2015 16:52
Show Gist options
  • Save JaiHirsch/cada9eaac3e24ef5a62a to your computer and use it in GitHub Desktop.
Save JaiHirsch/cada9eaac3e24ef5a62a to your computer and use it in GitHub Desktop.
MongoDB 3.0 java driver Codec example - main class to run the Grades Codec example
package org.scratch;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.MongoCollection;
public class GradeInserter {
private static MongoClient mc;
public static void main(String[] args) {
Codec<Document> defaultDocumentCodec = MongoClient.getDefaultCodecRegistry().get(
Document.class);
GradesCodec gradeCodec = new GradesCodec(defaultDocumentCodec);
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
MongoClient.getDefaultCodecRegistry(), CodecRegistries.fromCodecs(gradeCodec));
MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry)
.build();
mc = new MongoClient("localhost:27017", options);
MongoCollection<Grades> collection = mc.getDatabase("test").getCollection("test",
Grades.class);
collection.insertOne(new Grades(2, "Homework", 99.9));
Grades first = collection.find(Grades.class).first();
System.out.println(first);
mc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment