4 #include <SimoxUtility/json.h>
12 namespace bbuilder = bsoncxx::builder;
13 namespace bdoc = bsoncxx::document;
20 std::string
ret = segmentName;
24 ret = simox::alg::replace_all(
ret,
s, r);
32 std::string
ret = escapedName;
37 ret = simox::alg::replace_all(
ret, r,
s);
43 insert(mongocxx::collection& coll,
const nlohmann::json&
value)
45 std::string
v =
value.dump();
47 auto res = coll.insert_one(
q.view());
52 std::optional<nlohmann::json>
56 std::string
v =
value.dump();
58 auto res = coll.find_one(
q.view());
68 const nlohmann::json& query,
69 const nlohmann::json&
update)
74 auto u = bbuilder::basic::make_document(bbuilder::basic::kvp(
"$set", udoc));
76 auto res = coll.update_one(
q.view(), u.view());
82 std::optional<mongocxx::database>
95 const std::string& databaseName,
96 bool createIfNotExistent)
101 if (!createIfNotExistent)
103 throw armarx::LocalException(
"Database existence can not be ensured: " +
107 return client[databaseName];
110 std::optional<mongocxx::collection>
113 if (db.has_collection(collectionName))
115 return db[collectionName];
122 const std::string& collectionName,
123 bool createIfNotExistent)
128 if (!createIfNotExistent)
130 throw armarx::LocalException(
"Collection existence can not be ensured: " +
134 return db[collectionName];
140 if (
id.hasTimestamp())
142 return id.timestampStr();
146 throw armarx::LocalException(
"Called toDocumentName() on non-snapshot id: " +
id.
str());
153 std::stringstream ss;
154 if (
id.hasMemoryName())
158 if (
id.hasCoreSegmentName())
162 if (
id.hasProviderSegmentName())
166 if (
id.hasEntityName())
174 std::optional<nlohmann::json>
182 const nlohmann::json& json,
183 bool createIfNotExistent)
188 if (!createIfNotExistent)
190 throw armarx::LocalException(
"Document existence can not be ensured: " +
203 throw armarx::LocalException(
204 "Even after creating the docuemnt it wasnt found.. error: " + json.dump());
214 ARMARX_CHECK((
bool)doc) <<
" Could not find document matching: " << json.dump()
215 <<
" in collection " << collection.name();
222 const nlohmann::json& query,
223 const nlohmann::json&
update)
238 std::vector<nlohmann::json>
241 std::vector<nlohmann::json>
ret;
242 mongocxx::cursor cursor = collection.find({});
243 for (
const auto& doc : cursor)
246 nlohmann::json::parse(
bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed)));