4 #include <SimoxUtility/algorithm/string/string_tools.h>
5 #include <SimoxUtility/json.h>
13 namespace bbuilder = bsoncxx::builder;
14 namespace bdoc = bsoncxx::document;
21 std::string
ret = segmentName;
25 ret = simox::alg::replace_all(
ret,
s, r);
33 std::string
ret = escapedName;
38 ret = simox::alg::replace_all(
ret, r,
s);
44 insert(mongocxx::collection& coll,
const nlohmann::json&
value)
46 std::string
v =
value.dump();
48 auto res = coll.insert_one(
q.view());
53 std::optional<nlohmann::json>
57 std::string
v =
value.dump();
59 auto res = coll.find_one(
q.view());
69 const nlohmann::json& query,
70 const nlohmann::json&
update)
75 auto u = bbuilder::basic::make_document(bbuilder::basic::kvp(
"$set", udoc));
77 auto res = coll.update_one(
q.view(), u.view());
83 std::optional<mongocxx::database>
96 const std::string& databaseName,
97 bool createIfNotExistent)
102 if (!createIfNotExistent)
104 throw armarx::LocalException(
"Database existence can not be ensured: " +
108 return client[databaseName];
111 std::optional<mongocxx::collection>
114 if (db.has_collection(collectionName))
116 return db[collectionName];
123 const std::string& collectionName,
124 bool createIfNotExistent)
129 if (!createIfNotExistent)
131 throw armarx::LocalException(
"Collection existence can not be ensured: " +
135 return db[collectionName];
141 if (
id.hasTimestamp())
143 return id.timestampStr();
147 throw armarx::LocalException(
"Called toDocumentName() on non-snapshot id: " +
id.
str());
154 std::stringstream ss;
155 if (
id.hasMemoryName())
159 if (
id.hasCoreSegmentName())
163 if (
id.hasProviderSegmentName())
167 if (
id.hasEntityName())
175 std::optional<nlohmann::json>
183 const nlohmann::json& json,
184 bool createIfNotExistent)
189 if (!createIfNotExistent)
191 throw armarx::LocalException(
"Document existence can not be ensured: " +
204 throw armarx::LocalException(
205 "Even after creating the docuemnt it wasnt found.. error: " + json.dump());
215 ARMARX_CHECK((
bool)doc) <<
" Could not find document matching: " << json.dump()
216 <<
" in collection " << collection.name();
223 const nlohmann::json& query,
224 const nlohmann::json&
update)
239 std::vector<nlohmann::json>
242 std::vector<nlohmann::json>
ret;
243 mongocxx::cursor cursor = collection.find({});
244 for (
const auto& doc : cursor)
247 nlohmann::json::parse(
bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed)));