diff options
author | neodarz <neodarz@neodarz.net> | 2019-08-09 16:28:59 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-08-09 16:28:59 +0200 |
commit | cf86e3de524ff247f496c34cd9e8df61cd4e6845 (patch) | |
tree | c7070f754e7d2d7f238bcc776161628f18998c21 /db | |
parent | a6408962010c1880a54d2f0f3a1e35b0ca65531d (diff) | |
download | umosapicpp-cf86e3de524ff247f496c34cd9e8df61cd4e6845.tar.xz umosapicpp-cf86e3de524ff247f496c34cd9e8df61cd4e6845.zip |
Add ability to search uobjects
Diffstat (limited to '')
-rw-r--r-- | db/uobject.cpp | 22 | ||||
-rw-r--r-- | db/uobject.h | 1 |
2 files changed, 23 insertions, 0 deletions
diff --git a/db/uobject.cpp b/db/uobject.cpp index 0b76daf..bfaf7df 100644 --- a/db/uobject.cpp +++ b/db/uobject.cpp @@ -102,3 +102,25 @@ std::string uobject::remove(std::string collection, std::string oid, struct json } return json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); } + +std::string uobject::searchKeyValue(std::string collection, std::string key, std::string value, struct json_object* jsonArray) { + auto conn = mongo.get_connection(); + + auto coll = (*conn)[config["mongo_db"]][collection]; + + // Generate a string with the folowing format: + // {"datas.key":"value"} + std::string json_string = "{\"datas."; + json_string += key; + json_string += "\":\""; + json_string += value; + json_string += "\"}"; + + auto cursor = coll.find(bsoncxx::from_json(json_string)); + + for (auto&& doc : cursor) { + json_object_array_add(jsonArray, json_tokener_parse(bsoncxx::to_json(doc).c_str())); + } + + return json_object_to_json_string_ext(jsonArray, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); +} diff --git a/db/uobject.h b/db/uobject.h index 3e0fd1e..0533661 100644 --- a/db/uobject.h +++ b/db/uobject.h @@ -23,6 +23,7 @@ namespace uobject { std::string retrieveAll(std::string collection, struct json_object* jsonObjects); std::string add(std::string collection, struct json_object* jsonObjects, const char * body); std::string remove(std::string collection, std::string oid, struct json_object* jsonObjects); + std::string searchKeyValue(std::string collection, std::string key, std::string value, struct json_object* jsonArray); } #endif |