1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include "mongo/client/dbclient.h"
#include <iostream>
#ifndef verify #define verify(x) MONGO_verify(x) #endif
using namespace std; using namespace mongo;
int main(int argc, char* argv[]) { if (argc > 2) { std::cout << "usage: " << argv[0] << " [MONGODB_URI]" << std::endl; return EXIT_FAILURE; } mongo::client::GlobalInstance instance; if (!instance.initialized()) { std::cout << "failed to initialize the client driver: " << instance.status() << std::endl; return EXIT_FAILURE; }
std::string uri = argc == 2 ? argv[1] : "mongodb://127.0.0.1:27017"; std::string errmsg;
ConnectionString cs = ConnectionString::parse(uri, errmsg);
if (!cs.isValid()) { std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl; return EXIT_FAILURE; }
boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg)); if (!conn) { cout << "couldn't connect : " << errmsg << endl; return EXIT_FAILURE; }
const char* ns = "test.first";
conn->dropCollection(ns); conn->remove(ns, BSONObj()); verify(conn->findOne(ns, BSONObj()).isEmpty()); cout << "(1) test insert----" << endl; conn->insert(ns, BSON("name" << "joe" << "pwd" << "123456" << "age" << 20)); verify(!conn->findOne(ns, BSONObj()).isEmpty()); cout << "insert data : " << conn->findOne(ns, BSONObj()) << endl; cout << "insert success!" << endl;
cout << "(2) test remove----" << endl; conn->remove(ns, BSONObj()); verify(conn->findOne(ns, BSONObj()).isEmpty()); cout << "remove success!" << endl; conn->insert(ns, BSON("name" << "joe" << "pwd" << "234567" << "age" << 21)); { BSONObj res = conn->findOne(ns, BSONObj()); verify(strstr(res.getStringField("name"), "joe")); verify(!strstr(res.getStringField("name2"), "joe")); verify(21 == res.getIntField("age")); } cout << "insert data : " << conn->findOne(ns, BSONObj()) <<endl;
{ BSONObj res = conn->findOne(ns, BSONObjBuilder().append("name", "joe").obj()); verify(!strstr(res.getStringField("name2"), "jeo")); BSONObj after = BSONObjBuilder().appendElements(res).append("name2", "w").obj(); cout << "(3) test update name joe add name2 name3----" << endl; conn->update(ns, BSONObjBuilder().append("name", "joe").obj(), after); verify(!strstr(res.getStringField("name2"), "joe")); verify(conn->findOne(ns, BSONObjBuilder().append("name", "joe2").obj()).isEmpty()); cout << " update1 data : " << conn->findOne(ns, Query("{name2:'w'}")) << endl; const string TEST_NS = "test.first"; conn->update("test.first", res, BSON("$set" << BSON("age" << 11))); cout << " update2 data : " << conn->findOne(ns, BSONObj()) << endl; res = conn->findOne(ns, BSONObjBuilder().append("name", "joe").obj());
try { after = BSONObjBuilder().appendElements(res).append("name3", "h").obj(); conn->update(ns, BSONObjBuilder().append("name", "joe").obj(), after); } catch (OperationException&) { cout << " update error: " << conn->getLastErrorDetailed().toString() << endl; } verify(!conn->findOne(ns, BSONObjBuilder().append("name", "joe").obj()).isEmpty()); cout << " update3 data : " << conn->findOne(ns, Query("{name3:'h'}")) << endl;
cout << "(4) test query-----" << "\n query data : " << conn->findOne(ns, BSONObjBuilder().append("name", "joe").obj()) << endl; cout << " Query data : " << conn->findOne(ns, Query("{name:'joe'}")) << endl; }
return 0; }
|