mongodb c++ QString to kvp() value

321 views Asked by At

I'm struggling to get a valid conversion of my qstring to whatever kvp needs inside mongo...

I tried

std::string_view k(documentKey.toStdString().c_str());//, documentKey.size()); // tried adding size as wll
std::string_view v(documentId.toStdString().c_str());//, documentId.size());

and then:

auto doc = mCollection.find(make_document(kvp(k, v)));

But I keep on getting empty results. If I type directly k/v as "someKey", "someValue" it works just fine. But I can't figure out how to convert it from one to another... any ideas?

TIA

1

There are 1 answers

1
SPM On

kvp can be built with two strings, so if .toStdString() returns a valid string this should work for you:

std::string docKey = documentKey.toStdString();
std::string docId = documentId.toStdString();
auto doc = mCollection.find(make_document(kvp(docKey, docId)));

Please keep in mind that if the _id is automatically generated by mongo you'll need to use an ObjectId type instead.