Skip to content

Instantly share code, notes, and snippets.

View dalmat36's full-sized avatar

Matt Dalesio dalmat36

View GitHub Profile
@dalmat36
dalmat36 / InsertPeoplePrintResults
Created April 3, 2015 16:29
This code snippet inserts three people documents into the MongoDB "people" collection and prints out all people in the collection.
collection.insertOne(person1);
collection.insertOne(person2);
collection.insertOne(person3);
System.out.println("Result After Insert\n");
printAllPeople();
@dalmat36
dalmat36 / FindAndReplaceDocument
Created April 3, 2015 16:35
This code snippet shows how to find an existing document in a the MongoDB "people" collection and replace it.
//Update a person document by Id
Document updatedPerson = new Document("First Name", "Marilyn")
.append("Last Name", "Monroe")
.append("Description", "Actor");
collection.findOneAndReplace(eq("First Name", "Marilyn"), updatedPerson);
System.out.println("Result After Update\n");
printAllPeople();
@dalmat36
dalmat36 / RemoveDocumentFromCollection
Created April 3, 2015 16:39
This code snippet shows how to remove a person document from the MongoDB "people" collection.
//Remove document from collection
collection.deleteOne(eq("First Name", "Marilyn"));
System.out.println("Result After Delete\n");
printAllPeople();
@dalmat36
dalmat36 / FindAndPrintAllPeople
Created April 3, 2015 16:41
This code snippet finds all people documents in the "people" collection and prints them to the console.
private static void printAllPeople(){
MongoCursor<Document> people = collection.find().iterator();
while(people.hasNext())
System.out.println(people.next());
System.out.println("\n");
people.close();
}
@dalmat36
dalmat36 / SimpleCRUDWithMonogDB
Created April 3, 2015 16:44
This example shows how to perform some basic CRUD functions with MongoDB and the Java Driver
package mongoCrud;
import static com.mongodb.client.model.Filters.eq;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
@dalmat36
dalmat36 / AbstractIndexedCollectionHandler
Created April 9, 2015 16:01
This code snippet creates and abstract indexed collection handler. This enables you to use Commons DBUtils to return a result from a database as a CQEngine indexed collection.
public abstract class AbstractIndexedCollectionHandler<T> implements ResultSetHandler<IndexedCollection<T>> {
public IndexedCollection<T> handle(ResultSet rs) throws SQLException {
IndexedCollection<T> rows = CQEngine.newInstance();
while (rs.next()) {
rows.add(this.handleRow(rs));
}
return rows;
}
@dalmat36
dalmat36 / MongoDB Insert Post 76jCKfW2Mr8
Created May 6, 2015 14:26
Webinar: Introduction to using MongoDB and Spring Data on Cloud Foundry Insert Post
p = {author: "roger",
date: new Date(),
text: "about mongodB...",
tags: ["tech", "databases"]}
db.posts.save(p)
@dalmat36
dalmat36 / MongoDB Find Pretty 76jCKfW2Mr8
Last active August 29, 2015 14:20
Webinar: Introduction to using MongoDB and Spring Data on Cloud Foundry Find pretty
db.posts.find().pretty()
Result:
{
"_id" : ObjectId("554a2380df506c53b1a78647"),
"author" : "roger",
"date" : ISODate("2015-05-06T14:21:24.537Z"),
"text" : "about mongodB...",
"tags" : [
"tech",
@dalmat36
dalmat36 / Secondary Indexes 76jCKfW2Mr8
Created May 6, 2015 14:52
Webinar: Introduction to using MongoDB and Spring Data on Cloud Foundry Secondary Indexes
//1 means ascending, -1 means descending
db.posts.ensureIndex({author:1})
db.posts.find({author:'roger'}).pretty()
Result:
{
"_id" : ObjectId("554a2380df506c53b1a78647"),
"author" : "roger",
"date" : ISODate("2015-05-06T14:21:24.537Z"),
"text" : "about mongodB...",
@dalmat36
dalmat36 / Conditional Query Operators 76jCKfW2Mr8
Created May 6, 2015 14:55
Webinar: Introduction to using MongoDB and Spring Data on Cloud Foundry
//find posts with any tags
db.posts.find({tags:{$exists:true}}).pretty()
//find posts matching a regular expression
db.posts.find({author:/^rog*/i}).pretty()
//count posts by author
db.posts.find({author: 'roger'}).count()