Hi everyone, happy to join this forum!
I have a MongoDB document with the following structure:
{
"_id":"$oid":"621fbaeaeedd1c000e60fbd2"},
"username":"myuser",
"password":"mypassword",
"comments":["comment1", "comment2", "comment3"]
}
I have a vector of comments:
std::vector<std::string> mycomments;
mycomments.push_back("comment2");
mycomments.push_back("comment4");
I would like to insert, if it is not already present, each string of the vector “mycomments” into the array “comments” in MongoDB document. I think that I should use the update_one function.
I would like to obtain this final result:
{
"_id":"$oid":"621fbaeaeedd1c000e60fbd2"},
"username":"myuser",
"password":"mypassword",
"comments":["comment1", "comment2", "comment3", "comment4"]
}
I tried to do it but I don’t know how to proceed:
mongocxx::collection coll = db["users"];
coll.update_one(
document{} << "username" << username
<< finalize,
document{} << ???
<< open_document
<< "comments" << ???
<< close_document << finalize);
Thanks in advance.