mongodb.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <string>
6 
7 #include <SimoxUtility/json.h>
8 
10 
11 
12 #include <mongocxx/client.hpp>
13 #include <mongocxx/instance.hpp>
14 #include <mongocxx/pool.hpp>
15 #include <mongocxx/stdx.hpp>
16 #include <mongocxx/uri.hpp>
17 #include <bsoncxx/builder/stream/array.hpp>
18 #include <bsoncxx/builder/stream/document.hpp>
19 #include <bsoncxx/builder/stream/helpers.hpp>
20 #include <bsoncxx/json.hpp>
21 
23 {
24 
25  namespace detail
26  {
27  inline const std::map<std::string, std::string> EscapeTable = {{"/", "|"}};
28 
29  std::string escapeName(const std::string& segmentName);
30 
31  std::string unescapeName(const std::string& escapedName);
32 
33  bool insert(mongocxx::collection& coll, const nlohmann::json& value);
34 
35  std::optional<nlohmann::json> contains(mongocxx::collection& coll,
36  const nlohmann::json& value);
37 
38  bool update(mongocxx::collection& coll,
39  const nlohmann::json& query,
40  const std::string& vkey,
41  const nlohmann::json& vdata);
42  } // namespace detail
43 
44  std::string toDocumentID(const armem::MemoryID&);
45  std::string toCollectionName(const armem::MemoryID&);
46 
47  std::optional<mongocxx::database> databaseExists(mongocxx::client&,
48  const std::string& databaseName);
49  mongocxx::database ensureDatabaseExists(mongocxx::client&,
50  const std::string& databaseName,
51  bool createIfNotExistent);
52 
53  std::optional<mongocxx::collection> collectionExists(mongocxx::database&,
54  const std::string& collectionName);
55  mongocxx::collection ensureCollectionExists(mongocxx::database&,
56  const std::string& collectionName,
57  bool createIfNotExistent);
58 
59  std::optional<nlohmann::json> documentExists(mongocxx::collection& collection,
60  const nlohmann::json& query);
61 
62  nlohmann::json ensureDocumentExists(mongocxx::collection& collection,
63  const nlohmann::json& query,
64  bool createIfNotExistent = false);
65 
66  nlohmann::json readDataFromDocument(mongocxx::collection& collection,
67  const nlohmann::json& query);
68 
69  void writeDataToDocument(mongocxx::collection& collection,
70  const nlohmann::json& query,
71  const nlohmann::json& data);
72 
73  std::vector<nlohmann::json> getAllDocuments(mongocxx::collection& collection);
74 
75 } // namespace armarx::armem::server::ltm::util::mongodb
armarx::armem::server::ltm::util::mongodb::ensureDocumentExists
nlohmann::json ensureDocumentExists(mongocxx::collection &collection, const nlohmann::json &json, bool createIfNotExistent)
Definition: mongodb.cpp:181
armarx::armem::server::ltm::util::mongodb::toDocumentID
std::string toDocumentID(const armem::MemoryID &id)
Definition: mongodb.cpp:138
armarx::armem::server::ltm::util::mongodb::databaseExists
std::optional< mongocxx::database > databaseExists(mongocxx::client &client, const std::string &databaseName)
Definition: mongodb.cpp:83
MemoryID.h
armarx::armem::server::ltm::util::mongodb::readDataFromDocument
nlohmann::json readDataFromDocument(mongocxx::collection &collection, const nlohmann::json &json)
Definition: mongodb.cpp:210
armarx::armem::server::ltm::util::mongodb::detail::contains
std::optional< nlohmann::json > contains(mongocxx::collection &coll, const nlohmann::json &value)
Definition: mongodb.cpp:53
detail
Definition: OpenCVUtil.cpp:127
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::armem::server::ltm::util::mongodb::detail::escapeName
std::string escapeName(const std::string &segmentName)
Definition: mongodb.cpp:18
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::ltm::util::mongodb::getAllDocuments
std::vector< nlohmann::json > getAllDocuments(mongocxx::collection &collection)
Definition: mongodb.cpp:239
armarx::armem::server::ltm::util::mongodb::detail::insert
bool insert(mongocxx::collection &coll, const nlohmann::json &value)
Definition: mongodb.cpp:43
armarx::armem::server::ltm::util::mongodb
Definition: mongodb.cpp:10
armarx::armem::server::ltm::util::mongodb::documentExists
std::optional< nlohmann::json > documentExists(mongocxx::collection &collection, const nlohmann::json &json)
Definition: mongodb.cpp:175
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::armem::server::ltm::util::mongodb::writeDataToDocument
void writeDataToDocument(mongocxx::collection &collection, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:221
armarx::armem::server::ltm::util::mongodb::ensureCollectionExists
mongocxx::collection ensureCollectionExists(mongocxx::database &db, const std::string &collectionName, bool createIfNotExistent)
Definition: mongodb.cpp:121
armarx::armem::server::ltm::util::mongodb::detail::EscapeTable
const std::map< std::string, std::string > EscapeTable
Definition: mongodb.h:27
armarx::armem::server::ltm::util::mongodb::toCollectionName
std::string toCollectionName(const armem::MemoryID &id)
Definition: mongodb.cpp:150
armarx::armem::server::ltm::util::mongodb::collectionExists
std::optional< mongocxx::collection > collectionExists(mongocxx::database &db, const std::string &collectionName)
Definition: mongodb.cpp:111
armarx::armem::server::ltm::util::mongodb::ensureDatabaseExists
mongocxx::database ensureDatabaseExists(mongocxx::client &client, const std::string &databaseName, bool createIfNotExistent)
Definition: mongodb.cpp:94
armarx::armem::server::ltm::util::mongodb::detail::unescapeName
std::string unescapeName(const std::string &escapedName)
Definition: mongodb.cpp:30