Created
March 6, 2016 11:54
-
-
Save OlegJakushkin/cc0a22298d1db3d42116 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* simple mongo 2.6.x client test | |
mongo server https://goo.gl/x1PJlI | |
mongo client libraries https://goo.gl/Ggpdcd | |
requiers packages for x64 | |
Install-Package boost -Version 1.55.0.16 | |
Install-Package boost_system-vc110 -Version 1.55.0.16 | |
Install-Package boost_date_time-vc110 -Version 1.55.0.16 | |
Install-Package boost_thread-vc110 -Version 1.55.0.16 | |
Install-Package boost_chrono-vc110 -Version 1.55.0.16 | |
Install-Package boost_filesystem-vc110 -Version 1.55.0.16 | |
*/ | |
#define _CRT_SECURE_NO_WARNINGS | |
#define STATIC_LIBMONGOCLIENT | |
#include <iostream> | |
#include <string> | |
#include <mongo/bson/bson.h> | |
#include <mongo/client/dbclient.h> | |
#include <thread> | |
using namespace std; | |
int main(){ | |
try { | |
mongo::client::initialize(); | |
mongo::DBClientConnection connection; | |
connection.connect("localhost"); | |
mongo::BSONObjBuilder vasiasBuilder; | |
vasiasBuilder.append("name", "vasya"); | |
vasiasBuilder.append("age", "1"); | |
auto vasia = vasiasBuilder.obj(); | |
cout << vasia.jsonString() << endl; | |
connection.insert("test.persons", vasia); | |
auto annsBuilder = mongo::BSONObjBuilder(); | |
annsBuilder.append("name", "ann"); | |
annsBuilder.append("age", "5"); | |
auto ann = annsBuilder.obj(); | |
cout << ann.jsonString() << endl; | |
connection.insert("test.persons", ann); | |
this_thread::sleep_for(chrono::milliseconds(1000)); | |
cout << "count:" << connection.count("test.persons") << endl; | |
auto cursor = connection.query("test.persons", mongo::fromjson("{'age': { '$gt' : '1'} }")); //$gt == больше, остальные ключевые классификаторы тут https://docs.mongodb.org/manual/reference/operator/query-comparison/ | |
cout << "found objects:" << endl; | |
while (cursor->more()) { | |
cout << cursor->next().toString() << endl; | |
} | |
connection.remove("test.persons", mongo::fromjson("{}")); | |
} catch( const exception &e ) { | |
cout << "caught " << e.what() << endl; | |
} | |
cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment