[CXX-877] Have no idea how to add bsoncxx::document::value to another document Created: 24/Mar/16  Updated: 08/Jan/24  Resolved: 29/Mar/16

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 3.0.1

Type: Bug Priority: Major - P3
Reporter: Pavel Odintsov Assignee: Matt Cotter
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CXX-860 Fail Combine Multiple Nested Queries ... Closed

 Description   

Hello, folks!

I'm using recent C++ driver (it's awesome)! But after deep reading of all examples I could not find any way how to add bsoncxx::document::value into existing document.

I'm doing following:

  auto db = conn[fastnetmon_global_configuration.mongodb_database_name];
        std::string collection_name = "mitigations";
 
        auto doc = bsoncxx::builder::basic::document{};
        doc.append(bsoncxx::builder::basic::kvp("active", bsoncxx::types::b_bool{true}));
        doc.append(bsoncxx::builder::basic::kvp("mitigation_type", "blackhole"));
 
       bsoncxx::document::value details = bsoncxx::from_json(json_encoded_flow_spec);     

But obvious try to add "details" as sub document failed:

doc.append(bsoncxx::builder::basic::kvp("details", details);

Could you help me? There are no docs in examples folder about adding another document cooked somewhere else.



 Comments   
Comment by Githook User [ 29/Mar/16 ]

Author:

{u'username': u'Machyne', u'name': u'Matt Cotter', u'email': u'matt.cotter@mongodb.com'}

Message: CXX-877 add example for nested bson document
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/f418feb19b46c279ec87cd31f34ae2d991d5ba34

Comment by Andrew Morrow (Inactive) [ 24/Mar/16 ]

pavel.odintsov - Thanks! We are really excited about the new C++11 driver, and we really appreciate that you are filing tickets and asking questions - it helps us make the driver better for everyone. You are an early adopter!

You might want to try posting to the mongodb-user group if you have questions going forward - we are monitoring that too will definitely answer questions posted there, but those questions and answers will get more visibility than a ticket that you file that only we see. Of course, if you have suggestion for enhancements, or real bugs or problems, filing them here definitely is the right thing to do. Or if your are happier filing JIRA tickets, that is of course fine too.

Comment by Pavel Odintsov [ 24/Mar/16 ]

That's really cool! Thanks for lighting fast answer!

To be honest I never saw anywhere else something like speed and involvement of MongoDB team

Have implemented this!

        auto doc = bsoncxx::builder::stream::document{};
        doc << "active" << bsoncxx::types::b_bool{true};
        doc << "mitigation_source" << get_printable_attack_detection_source(attack_detection_source);
        // TODO: we should offer real host IP here, really!
        doc << "affected_host" << std::string("0.0.0.0") + "/32";
        doc << "mitigation_type" << "flow_spec";
        doc << "mitigation_uuid" << flow_spec_rule.get_announce_uuid_as_string();
 
        std::string json_encoded_flow_spec;
        bool flow_json_encode_result = encode_flow_spec_to_json(flow_spec_rule, json_encoded_flow_spec);
 
        if (!flow_json_encode_result) {
            logger << log4cpp::Priority::ERROR << "Could not encode flow spec to json";
            return false;
        }
 
        try {
            bsoncxx::document::value details = bsoncxx::from_json(json_encoded_flow_spec);
 
            doc << "details" << bsoncxx::builder::stream::open_document << bsoncxx::builder::concatenate_doc{ details.view() } << bsoncxx::builder::stream::close_document;
        } catch (...) {
            logger << log4cpp::Priority::ERROR << "Could not load json to bson document";
            return false;
        }

Comment by Matt Cotter [ 24/Mar/16 ]

Hi pavel.odintsov,

I opened a pull request to add an example for just that: https://github.com/mongodb/mongo-cxx-driver/pull/475
Let me know if this makes it clear! Also, please see CXX-860 for more discussion on this.

Best,
-Matt

Comment by Matt Cotter [ 24/Mar/16 ]

Pull request: https://github.com/mongodb/mongo-cxx-driver/pull/475

Generated at Wed Feb 07 22:00:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.