This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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...", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| db.posts.find().pretty() | |
| Result: | |
| { | |
| "_id" : ObjectId("554a2380df506c53b1a78647"), | |
| "author" : "roger", | |
| "date" : ISODate("2015-05-06T14:21:24.537Z"), | |
| "text" : "about mongodB...", | |
| "tags" : [ | |
| "tech", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| p = {author: "roger", | |
| date: new Date(), | |
| text: "about mongodB...", | |
| tags: ["tech", "databases"]} | |
| db.posts.save(p) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static void printAllPeople(){ | |
| MongoCursor<Document> people = collection.find().iterator(); | |
| while(people.hasNext()) | |
| System.out.println(people.next()); | |
| System.out.println("\n"); | |
| people.close(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Remove document from collection | |
| collection.deleteOne(eq("First Name", "Marilyn")); | |
| System.out.println("Result After Delete\n"); | |
| printAllPeople(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| collection.insertOne(person1); | |
| collection.insertOne(person2); | |
| collection.insertOne(person3); | |
| System.out.println("Result After Insert\n"); | |
| printAllPeople(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Insert three famous person documents | |
| //Top 3 from http://www.biographyonline.net/people/famous-100.html | |
| Document person1 = new Document("First Name", "Marilyn") | |
| .append("Last Name", "Monroe"); | |
| Document person2 = new Document("First Name", "Abraham") | |
| .append("Last Name", "Lincoln"); | |
| Document person3 = new Document("First Name", "Mother") | |
| .append("Last Name", "Teresa"); |