/java/JavaSimpleExample.java

https://github.com/mongolab/mongodb-driver-examples · Java · 112 lines · 60 code · 27 blank · 25 comment · 1 complexity · 2398b0338c6b30e0becdf4de79f948ce MD5 · raw file

  1. /*
  2. * Copyright (c) 2017 ObjectLabs Corporation
  3. * Distributed under the MIT license - http://opensource.org/licenses/MIT
  4. *
  5. * Written with mongo-3.4.2.jar
  6. * Documentation: http://api.mongodb.org/java/
  7. * A Java class connecting to a MongoDB database given a MongoDB Connection URI.
  8. */
  9. import java.net.UnknownHostException;
  10. import com.mongodb.MongoClient;
  11. import com.mongodb.MongoClientURI;
  12. import com.mongodb.ServerAddress;
  13. import com.mongodb.client.MongoDatabase;
  14. import com.mongodb.client.MongoCollection;
  15. import org.bson.Document;
  16. import java.util.Arrays;
  17. import com.mongodb.Block;
  18. import com.mongodb.client.MongoCursor;
  19. import static com.mongodb.client.model.Filters.*;
  20. import com.mongodb.client.result.DeleteResult;
  21. import static com.mongodb.client.model.Updates.*;
  22. import com.mongodb.client.result.UpdateResult;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. public class JavaSimpleExample {
  26. public static void main(String[] args) throws UnknownHostException{
  27. // Create seed data
  28. List<Document> seedData = new ArrayList<Document>();
  29. seedData.add(new Document("decade", "1970s")
  30. .append("artist", "Debby Boone")
  31. .append("song", "You Light Up My Life")
  32. .append("weeksAtOne", 10)
  33. );
  34. seedData.add(new Document("decade", "1980s")
  35. .append("artist", "Olivia Newton-John")
  36. .append("song", "Physical")
  37. .append("weeksAtOne", 10)
  38. );
  39. seedData.add(new Document("decade", "1990s")
  40. .append("artist", "Mariah Carey")
  41. .append("song", "One Sweet Day")
  42. .append("weeksAtOne", 16)
  43. );
  44. // Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname
  45. MongoClientURI uri = new MongoClientURI("mongodb://user:pass@host:port/db");
  46. MongoClient client = new MongoClient(uri);
  47. MongoDatabase db = client.getDatabase(uri.getDatabase());
  48. /*
  49. * First we'll add a few songs. Nothing is required to create the
  50. * songs collection; it is created automatically when we insert.
  51. */
  52. MongoCollection<Document> songs = db.getCollection("songs");
  53. // Note that the insert method can take either an array or a document.
  54. songs.insertMany(seedData);
  55. /*
  56. * Then we need to give Boyz II Men credit for their contribution to
  57. * the hit "One Sweet Day".
  58. */
  59. Document updateQuery = new Document("song", "One Sweet Day");
  60. songs.updateOne(updateQuery, new Document("$set", new Document("artist", "Mariah Carey ft. Boyz II Men")));
  61. /*
  62. * Finally we run a query which returns all the hits that spent 10
  63. * or more weeks at number 1.
  64. */
  65. Document findQuery = new Document("weeksAtOne", new Document("$gte",10));
  66. Document orderBy = new Document("decade", 1);
  67. MongoCursor<Document> cursor = songs.find(findQuery).sort(orderBy).iterator();
  68. try {
  69. while (cursor.hasNext()) {
  70. Document doc = cursor.next();
  71. System.out.println(
  72. "In the " + doc.get("decade") + ", " + doc.get("song") +
  73. " by " + doc.get("artist") + " topped the charts for " +
  74. doc.get("weeksAtOne") + " straight weeks."
  75. );
  76. }
  77. } finally {
  78. cursor.close();
  79. }
  80. // Since this is an example, we'll clean up after ourselves.
  81. songs.drop();
  82. // Only close the connection when your app is terminating
  83. client.close();
  84. }
  85. }