1 package wrappers; 2 3 import java.io.BufferedReader; 4 import java.io.Console; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.util.Date; 8 9 public class TestInsert3 { 10 static boolean DEBUG = true; 11 static final Console c = System.console(); 12 static Mongo3Interface jdi = null; 13 main(String[] args)14 public static void main(String[] args) { 15 int rc; 16 String[] parms = new String[4]; 17 18 jdi = new Mongo3Interface(DEBUG); 19 20 parms[0] = getLine("URI: ", false); 21 parms[1] = getLine("Database: ", false); 22 parms[2] = null; 23 parms[3] = null; 24 25 if (parms[0] == null) 26 parms[0] = "mongodb://localhost:27017"; 27 28 if (parms[1] == null) 29 parms[1] = "test"; 30 31 rc = jdi.MongoConnect(parms); 32 33 if (rc == 0) { 34 Object bdoc = jdi.MakeDocument(); 35 36 if (jdi.DocAdd(bdoc, "_id", (Object) 1, 0)) 37 System.out.println(jdi.GetErrmsg()); 38 39 if (jdi.DocAdd(bdoc, "Name", (Object) "Smith", 0)) 40 System.out.println(jdi.GetErrmsg()); 41 42 if (jdi.DocAdd(bdoc, "Age", (Object) 39, 0)) 43 System.out.println(jdi.GetErrmsg()); 44 45 if (jdi.DocAdd(bdoc, "Pi", (Object) 3.14, 0)) 46 System.out.println(jdi.GetErrmsg()); 47 48 if (jdi.DocAdd(bdoc, "Phone", (Object) "{\"ext\":[4,5,7]}", 1)) 49 System.out.println(jdi.GetErrmsg()); 50 51 if (jdi.DocAdd(bdoc, "Scores", (Object) "[24,2,13]", 2)) 52 System.out.println(jdi.GetErrmsg()); 53 54 Object bar = jdi.MakeArray(); 55 56 for (int i = 0; i < 2; i++) 57 if (jdi.ArrayAdd(bar, i, (Object) (Math.random() * 10.0), 0)) 58 System.out.println(jdi.GetErrmsg()); 59 60 if (jdi.DocAdd(bdoc, "Prices", bar, 0)) 61 System.out.println(jdi.GetErrmsg()); 62 63 Object dat = new Date(); 64 65 if (jdi.DocAdd(bdoc, "Date", dat, 0)) 66 System.out.println(jdi.GetErrmsg()); 67 68 System.out.println(bdoc); 69 70 // Try to update 71 if (!jdi.GetCollection("updtest") && !jdi.FindColl(null, null)) { 72 if (jdi.CollDelete(true) < 0) 73 System.out.println(jdi.GetErrmsg()); 74 75 if (jdi.CollInsert(bdoc)) 76 System.out.println(jdi.GetErrmsg()); 77 78 Object updlist = jdi.MakeDocument(); 79 80 if (jdi.DocAdd(updlist, "Age", (Object) 40, 0)) 81 System.out.println(jdi.GetErrmsg()); 82 83 Object upd = jdi.MakeDocument(); 84 85 if (jdi.DocAdd(upd, "$set", updlist, 0)) 86 System.out.println(jdi.GetErrmsg()); 87 88 if (jdi.ReadNext() > 0 && jdi.CollUpdate(upd) < 0) 89 System.out.println(jdi.GetErrmsg()); 90 91 if (!jdi.Rewind() && jdi.ReadNext() > 0) 92 System.out.println(jdi.GetDoc()); 93 else 94 System.out.println("Failed Rewind"); 95 96 } // endif n 97 98 } // endif rc 99 100 } // end of main 101 102 // ================================================================== getLine(String p, boolean b)103 private static String getLine(String p, boolean b) { 104 String response; 105 106 if (c != null) { 107 // Standard console mode 108 if (b) { 109 response = new String(c.readPassword(p)); 110 } else 111 response = c.readLine(p); 112 113 } else { 114 // For instance when testing from Eclipse 115 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 116 117 System.out.print(p); 118 119 try { 120 // Cannot suppress echo for password entry 121 response = in.readLine(); 122 } catch (IOException e) { 123 response = ""; 124 } // end of try/catch 125 126 } // endif c 127 128 return (response.isEmpty()) ? null : response; 129 } // end of getLine 130 131 } 132