Ich beginne gerade mit Mongo, und ich habe Probleme, es zu bekommen, um ein Dokument in eine Sammlung zu schreiben. Ich kann nicht scheinen, von einem Dokument :: Wert zu einem string :: view_or_value zu konvertieren. Irgendwelche Hinweise zum Aussortieren dieser Typen? Ich habe versucht, den Doc_value direkt zu senden, aber das ist nicht gültig für den Insert.Mongo - einen View_or_value von einem Wert erhalten?
#include "stdafx.h"
#include <cstdint>
#include <iostream>
#include <vector>
#include <mongocxx/instance.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
int main()
{
mongocxx::instance instance{};
mongocxx::client client{ mongocxx::uri{} };
mongocxx::database db = client["mydb"];
bsoncxx::builder::stream::document builder{};
bsoncxx::document::value doc_value = builder
<< "name" << "MongoDB"
<< "type" << "database"
<< "count" << 1
<< "versions" << bsoncxx::builder::stream::open_array
<< "v3.2" << "v3.0" << "v2.6"
<< close_array
<< "info" << bsoncxx::builder::stream::open_document
<< "x" << 203
<< "y" << 102
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::finalize;
db.collection("cats").insert_one(bsoncxx::string::view_or_value(doc_value));
return 0;
}
AHA! Das hat es getan. Irgendein empfohlenes Lesen, um Intuition auf bsoncxx zu bauen? – Carbon
Es gibt ziemlich umfangreiche Beispiele und Komponententests und eine Menge Dokumentation hier: https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working-with-bson/ – acm