Skip to content

Latest commit

 

History

History
65 lines (50 loc) · 3.8 KB

Insert.md

File metadata and controls

65 lines (50 loc) · 3.8 KB

ThorsMongo

Insert:

This method uses the MongoDB API insert. Please read the MongoDB documentation for details.

        template<typename T>        InsertResult    insert(std::vector<T> const& data, InsertConfig const& config = InsertConfig{});
        template<typename... T>     InsertResult    insert(std::tuple<T...> const& data, InsertConfig const& config = InsertConfig{});

The insert() method inserts a container of individual records into a collection.

Note: All objects in the vector or tuple must be a type that has been marked as serializable via ThorsSerialize.

    // Example
    class BankAccount
    {
        // Make the serializable class a friend so it can accesses private members.
        friend class ThorsAnvil::Serialize::Traits<BankAccount>;
        std::string     name;
        std::uint64_t   accountId;
        std::uint64_t   balance;

        public:
            // public Members;
    };

    // Declare the class as serializable:
    ThorsAnvil_MakeTrait(BankAccount, name, accountId, balance);

Parameters:

The required parameter data can either be a std::vector<> or a std::tuple<>. Optional parameter that can be passed using the config object. Any optional parameters that are not explicitly set on the config object will not be sent to the server.

Parameter Description
data A container of serialazable objects that will be inserted into the collection
config.maxTimeMS Specifies a time limit in milliseconds. If you do not specify a value for maxTimeMS, operations will not time out. A value of 0 explicitly specifies the default unbounded behavior.

MongoDB terminates operations that exceed their allotted time limit using the same mechanism as db.killOp(). MongoDB only terminates an operation at one of its designated interrupt points.
config.writeConcern A document that expresses the write concern for the operation.
Omit to use the default write concern.
When issued on a sharded cluster, mongos converts the write concern of the renameCollection command and its helper db.collection.renameCollection() to "majority".
config.comment A user-provided comment to attach to this command.
config.ordered If true, then when an insert of a document fails, return without inserting any remaining documents listed in the inserts array.
If false, then when an insert of a document fails, continue to insert the remaining documents.
Defaults to true.
config.bypassDocumentValidation Enables insert to bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.

Returns:

The InsertResult contains the field inserted that is populated with a unique identifier for each object inserted. If the object has a member _id this is used otherwise one is automatically created on the client side and sent to the server. The result can also used to check the operation succeeded and on failure to get the error information.

Example:

        // Insert three bank accounts.
        // Use the C++ {} to automatically create a vector<>
        auto result = mongo["People"]["Home"].insert({LokiBankAccount, OdinBankAccount, ThorBankAccount});
        if (result) {                                           // Same as result.isOk()
            for (auto const& id: result.inserted) {
                std::cerr << "Inserted Id: " << id << "\n";
            }
        }
        else {
            std::cerr << "Remove Failed: " << result << "\n";   // Same as result.getHRErrorMessage()
        }