Not sure if the tags, or even the topic is right but if it isn’t please let me know and I will change it.
Otherwise, I have a pretty simple question. In Java, I am able to insert custom class objects such as Person
, Customer
, etc. into the collection by using the following code
Person person;
MongoCollection<Person> col = db.getCollection("MachineData", Person.class);
col.insertOne(person);
Although, I cannot seem to figure out how to accomplish this within C++. Would this be more of a question for a C++ forum? Is this not how it is intended to be used being Mongo isn’t an ORDB?
Currently, I have this
// Creating customer object
Customer *cust = new Customer("Apple", "California", "Los Angeles", "1 Apple Way", "00000", "Customer Name", "012-345-6789", "email@yahoo.com");
// Grabbing the collection
mongocxx::collection collection = db["Quotes"];
// Make the document to insert
auto doc_value = bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("name", "Name"),
bsoncxx::builder::basic::kvp("type", "database"),
bsoncxx::builder::basic::kvp("count", 1),
bsoncxx::builder::basic::kvp("versions", bsoncxx::builder::basic::make_array("v6.0", "v5.0", "v4.4", "v4.2", "v4.0", "v3.6")),
bsoncxx::builder::basic::kvp("info", bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("x", 203), bsoncxx::builder::basic::kvp("y", 102))));
auto doc_view = doc_value.view();
collection.insert_one(doc_view);
Which I generally received from this link, following along with the sections. If this is possible, I’d love to be lead into the direction of how to go about it. Thanks a lot!
Edit:
So I should have realized this prior to posting, but I noticed that
bsoncxx::builder::basic::kvp("info", bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("x", 203), bsoncxx::builder::basic::kvp("y", 102))));
creates an embedded document, which is somewhat of the object I am looking for because I believe? Is this the only way to accomplish how I did it in Java?