Skip to content

Instantly share code, notes, and snippets.

@OlegJakushkin
Created March 19, 2016 09:01
Show Gist options
  • Save OlegJakushkin/7093e79e0726c643fa00 to your computer and use it in GitHub Desktop.
Save OlegJakushkin/7093e79e0726c643fa00 to your computer and use it in GitHub Desktop.
/* 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
*/
#include "precompiled.h"
using namespace std;
int main(){
try {
mongo::client::initialize();
mongo::DBClientConnection connection;
connection.connect("172.27.216.175:6666");
for(int i = 0; i < 100; ++i) {
auto annsBuilder = mongo::BSONObjBuilder();
annsBuilder.append("login", "ann");
annsBuilder.append("passwd", "ann");
annsBuilder.append("age", i);
auto ann = annsBuilder.obj();
cout << ann.jsonString() << endl;
connection.insert("test.persons", ann);
}
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "coun:" << connection.count("test.persons") << endl;
auto cursor = connection.query("test.persons", mongo::fromjson("{ '$and' : [ {'age': { '$lt' : 100} }, {'age': { '$gt' : 10}} ] }")); //$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