Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package com.tengen;
  2.  
  3. import com.mongodb.*;
  4. import de.flapdoodle.embed.mongo.MongodExecutable;
  5. import de.flapdoodle.embed.mongo.MongodProcess;
  6. import de.flapdoodle.embed.mongo.MongodStarter;
  7. import de.flapdoodle.embed.mongo.config.IMongodConfig;
  8. import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
  9. import de.flapdoodle.embed.mongo.config.Net;
  10. import de.flapdoodle.embed.mongo.distribution.Version;
  11. import de.flapdoodle.embed.process.runtime.Network;
  12.  
  13. import java.io.IOException;
  14. import java.util.Arrays;
  15. import java.util.List;
  16.  
  17. public class UpdateRemoveTestMock {
  18. public static void main(String[] args) throws IOException {
  19.  
  20. MongodStarter starter = MongodStarter.getDefaultInstance();
  21.  
  22. int port = 12345;
  23. IMongodConfig mongodConfig = new MongodConfigBuilder()
  24. .version(Version.Main.PRODUCTION)
  25. .net(new Net(port, Network.localhostIsIPv6()))
  26. .build();
  27.  
  28. MongodExecutable mongodExecutable = null;
  29. MongodProcess mongod = null;
  30.  
  31. try {
  32. mongodExecutable = starter.prepare(mongodConfig);
  33. mongod = mongodExecutable.start();
  34.  
  35. Mongo mongo = new Mongo("localhost", port);
  36. DB db = mongo.getDB("course");
  37. DBCollection collection = db.getCollection("UpdateRemoveTest");
  38. collection.drop();
  39.  
  40. List<String> names = Arrays.asList("alice", "bobby", "cathy", "david", "ethan");
  41. for (String name : names) {
  42. collection.insert(new BasicDBObject("_id", name));
  43. }
  44.  
  45. // see scratch method
  46. scratch(collection);
  47.  
  48. printCollection(collection);
  49. } finally {
  50. if (mongod != null && mongod.isProcessRunning())
  51. mongod.stop();
  52. if (mongodExecutable != null)
  53. mongodExecutable.stop();
  54. }
  55. }
  56.  
  57. // these are all the statement I used throughout the lecture.
  58. private static void scratch(DBCollection collection) {
  59. collection.update(new BasicDBObject("_id", "alice"),
  60. new BasicDBObject("age", 24));
  61.  
  62. collection.update(new BasicDBObject("_id", "alice"),
  63. new BasicDBObject(new BasicDBObject("gender", "F")));
  64.  
  65. collection.update(new BasicDBObject("_id", "alice"),
  66. new BasicDBObject("$set", new BasicDBObject("age", 24)));
  67.  
  68. collection.update(new BasicDBObject("_id", "frank"),
  69. new BasicDBObject("$set", new BasicDBObject("age", 24)), true, false);
  70.  
  71. collection.update(new BasicDBObject(),
  72. new BasicDBObject("$set", new BasicDBObject("title", "Dr")), false, true);
  73.  
  74. collection.remove(new BasicDBObject("_id", "frank"));
  75. }
  76.  
  77. private static void printCollection(final DBCollection collection) {
  78. DBCursor cursor = collection.find().sort(new BasicDBObject("_id", 1));
  79. try {
  80. while (cursor.hasNext()) {
  81. System.out.println(cursor.next());
  82. }
  83. } finally {
  84. cursor.close();
  85. }
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement