Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. System.out.println("Dataset cleanup migration");
  4.  
  5. // set your MongoDB Cluster connection string
  6. // TODO> Ticket: Migration - set the cluster connection string.
  7. String mongoUri = "mongodb+srv://m220student:m220password@mflix-cpo7t.mongodb.net/test?retryWrites=true";
  8.  
  9. // instantiate database and collection objects
  10. MongoDatabase mflix = MongoClients.create(mongoUri).getDatabase("mflix");
  11. MongoCollection<Document> movies = mflix.getCollection("movies");
  12. Bson dateStringFilter = type("lastupdated", "string");
  13. String datePattern = "yyyy-MM-dd HH:mm:ss";
  14. // TODO> Ticket: Migration - create a query filter that finds all
  15. // documents that are required to be updated and the correct date
  16. // format pattern
  17. Bson queryFilter = new Document("lastupdated",new Document("$type",BsonType.STRING));
  18. SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
  19.  
  20. // create list of bulkWrites to be applied.
  21. List<WriteModel<Document>> bulkWrites = new ArrayList<>();
  22.  
  23. // iterate over the documents and apply the transformations.
  24. for (Document doc : movies.find(dateStringFilter)) {
  25.  
  26. // Apply lastupdate string to date conversion
  27. WriteModel<Document> updateDate = transformDates(doc, dateFormat);
  28. if (updateDate != null) {
  29. bulkWrites.add(updateDate);
  30. }
  31. }
  32.  
  33. // TODO> Ticket: Migration - create a query filter that finds
  34. // documents where `imdb.rating` is of type string
  35.  
  36. Bson ratingStringFilter = type("imdb.rating", "string");
  37. for (Document doc : movies.find(ratingStringFilter)) {
  38. // Apply "imdb.rating" string to number conversion
  39. WriteModel<Document> updateRating = transformRating(doc);
  40. if (updateRating != null) {
  41. bulkWrites.add(updateRating);
  42. }
  43. }
  44.  
  45. // execute the bulk update
  46. // TODO> Ticket: Migration - set the bulkWrite options
  47. BulkWriteOptions bulkWriteOptions = new BulkWriteOptions().ordered(false);
  48. if (bulkWrites.isEmpty()) {
  49. System.out.println("Nothing to update!");
  50. System.exit(0);
  51. }
  52.  
  53. BulkWriteResult bulkResult = movies.bulkWrite(bulkWrites, bulkWriteOptions);
  54. // output the number of updated documents
  55. System.out.println(
  56. MessageFormat.format("Updated {0} documents", bulkResult.getModifiedCount()));
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement