Skip to content

Instantly share code, notes, and snippets.

@drorasaf
Last active February 18, 2016 15:43
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 drorasaf/1f5183de67dedaafe948 to your computer and use it in GitHub Desktop.
Save drorasaf/1f5183de67dedaafe948 to your computer and use it in GitHub Desktop.
basic tutorial for mongodb for java client
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.client.FindIterable;
Client mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
MongoDatabase database = mongoClient.getDB("Tutorial");
MongoCollection collection = database.getCollection("customers");
Document person = new Document("_id", 1213456789)
.append("name", "John Dough")
.append("address",
new Document("street", "Over the rainbow St")
.append("city", "Somewhere")
.append("state", "CA")
.append("zip", 88888));
collection.insert(person);
Document query = new Document("_id", 1213456789);
FindIterable<Document> iterable = collection.find(query);
Document person = iterable.first();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment