Advertisement
smeacham

Untitled

Aug 9th, 2016
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package com.example.mongodb_demo;
  2.  
  3. import static com.mongodb.client.model.Filters.eq;
  4. import static com.mongodb.client.model.Filters.and;
  5. import static com.mongodb.client.model.Filters.or;
  6. import static com.mongodb.client.model.Sorts.ascending;
  7. import static java.util.Arrays.asList;
  8.  
  9. import java.text.DateFormat;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Locale;
  13.  
  14. import org.bson.Document;
  15.  
  16. import com.mongodb.Block;
  17. import com.mongodb.MongoClient;
  18. import com.mongodb.client.AggregateIterable;
  19. import com.mongodb.client.FindIterable;
  20. import com.mongodb.client.MongoCollection;
  21. import com.mongodb.client.MongoDatabase;
  22. import com.mongodb.client.model.Sorts;
  23. import com.mongodb.client.result.UpdateResult;
  24.  
  25. /**
  26.  * Hello world!
  27.  *
  28.  */
  29. public class App
  30. {  
  31.     private static void find(MongoCollection<Document> coll) {
  32.         // returns all documents
  33.         FindIterable<Document> i = coll.find();
  34.        
  35.         // return documents that match a query document
  36.         i = coll.find(new Document("borough", "Manhattan"));
  37.         // same query, using $eq
  38.         i = coll.find(eq("borough", "Manhattan"));
  39.        
  40.         // match subdocuments.
  41.         i = coll.find(new Document("address.zipcode", "10075"));
  42.         // same query, using $eq
  43.         i = coll.find(eq("address.zipcode", "10075"));
  44.  
  45.         // a compound query
  46.         i = coll.find(new Document("grades.score", new Document("$gt", 30)));
  47.        
  48.         // logical AND/OR
  49.         i = coll.find(new Document("cuisine", "Italian").append("address.zipcode", "10075"));
  50.         i = coll.find(and(eq("cuisine", "Italian"), eq("address.zipcode", "10075")));
  51.        
  52.         i = coll.find(new Document("$or", asList(
  53.                 new Document("cuisine", "Italian"),
  54.                 new Document("address.zipcode", "10075"))));
  55.         i = coll.find(or(eq("cuisine", "Italian"), eq("address.zipcode", "10075")));
  56.        
  57.         // Sort
  58.         i = coll.find().sort(new Document("borough", 1).append("address.zipcode", 1));
  59.         i = coll.find().sort(ascending("borough", "address.zipcode"));
  60.        
  61.         i.forEach(new Block<Document>() {
  62.             public void apply(Document t) {
  63.                 System.out.println(t);
  64.             }
  65.         });
  66.     }
  67.    
  68.     private static void aggregate(MongoCollection<Document> coll) {
  69.        
  70.         // How many restaurants in each borough?
  71.         AggregateIterable<Document> i = coll.aggregate(asList(
  72.                 new Document("$group", new Document("_id", "$borough").append("count", new Document("$sum", 1)))));
  73.        
  74.         // How many Brazilian restaurants in Queens are in each zipcode?
  75.         i = coll.aggregate(asList(
  76.                 new Document("$match", new Document("borough", "Queens").append("cuisine", "Brazilian")),
  77.                 new Document("$group", new Document("_id", "$address.zipcode").append("count", new Document("$sum", 1)))));
  78.        
  79.         i.forEach(new Block<Document>() {
  80.             public void apply(Document t) {
  81.                 System.out.println(t);
  82.             }
  83.         });
  84.     }
  85.    
  86.     private static void create(MongoCollection<Document> coll) throws ParseException {
  87.         DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
  88.        
  89.         coll.insertOne(
  90.                 new Document("address",
  91.                     new Document()
  92.                         .append("street", "2 Avenue")
  93.                         .append("zipcode", "10075")
  94.                         .append("building", "1480")
  95.                         .append("coord", asList(-73.9557313, 40.7720266)))
  96.                     .append("borough", "Manhattan")
  97.                     .append("cuisine", "Italian")
  98.                     .append("grades", asList(
  99.                             new Document()
  100.                                 .append("date", format.parse("2014-10-01T00:00:00Z"))
  101.                                 .append("grade", "A")
  102.                                 .append("score",  11),
  103.                             new Document()
  104.                                 .append("date", format.parse("2014-01-16T00:00:00Z"))
  105.                                 .append("grade", "B")
  106.                                 .append("score",  17)))
  107.                     .append("name", "Vella")
  108.                     .append("restaurant_id", "41704620"));
  109.     }
  110.  
  111.     private static UpdateResult update(MongoCollection<Document> coll) {
  112.         UpdateResult retval;
  113.        
  114.         retval = coll.updateOne(new Document("name", "Juni"),
  115.                 new Document("$set", new Document("cuisine", "American (New)"))
  116.                     .append("$currentDate", new Document("lastModified", true)));
  117.                
  118.         return retval;
  119.     }
  120.    
  121.     private static Document replace(MongoCollection<Document> coll) {
  122.         Document retval;
  123.        
  124.         retval = coll.findOneAndReplace(new Document("restaurant_id", "41704620"),
  125.                 new Document("address",
  126.                         new Document()
  127.                             .append("street", "2 Avenue")
  128.                             .append("zipcode", "10075")
  129.                             .append("building", "1480")
  130.                             .append("coord", asList(-73.9557313, 40.7720266)))
  131.                     .append("name", "Vella 2"));
  132.        
  133.         return retval;
  134.     }
  135.    
  136.     public static void main( String[] args ) throws ParseException
  137.     {
  138.         final MongoClient mc = new MongoClient();
  139.         final MongoDatabase db = mc.getDatabase("test");
  140.         final MongoCollection<Document> coll = db.getCollection("restaurants");
  141.        
  142.         // find(coll);
  143.         // aggregate(coll);
  144.         // create(coll);
  145.         // update(coll);
  146.         // replace(coll);
  147.        
  148.         mc.close();
  149.     }
  150.  
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement