Ejemplo n.º 1
0
    std::string collection::insert(nlohmann::json::object_t const& document) throw(std::runtime_error)
    {
        bson_oid_t oid;

        std::shared_ptr<bson> bson_doc = convert_to_bson(document);
        if(document.find("_id") != document.end()) {
            std::string const &id = document.find("_id")->second;
            if(!ejdbisvalidoidstr(id.c_str())) {
                throw ejdb_exception(JBEINVALIDBSONPK);
            }
            bson_oid_from_string(&oid, id.c_str());
            std::shared_ptr<bson> bson_oid(bson_create(), bson_del);
            bson_init(bson_oid.get());
            bson_append_oid(bson_oid.get(), "_id", &oid);
            bson_finish(bson_oid.get());
            std::shared_ptr<bson> bson_doc_with_oid(bson_create(), bson_del);
            bson_init(bson_doc_with_oid.get());
            bson_merge(bson_oid.get(), bson_doc.get(), true, bson_doc_with_oid.get());
            bson_finish(bson_doc_with_oid.get());
            bson_doc.swap(bson_doc_with_oid);
        }

        if(!ejdbsavebson(_coll.get(), bson_doc.get(), &oid)) {
            throw_last_ejdb_exception();
        }

        std::string id(24, '\0');
        bson_oid_to_string(&oid, &id[0]);

        document_added(id, document);
        return id;
    }
Ejemplo n.º 2
0
    nlohmann::json collection::modified_fields(nlohmann::json::object_t const& a, nlohmann::json::object_t const& b)
    {
        nlohmann::json::object_t diff;
        std::set_difference(b.begin(), b.end(), a.begin(), a.end(), std::inserter(diff, diff.begin()));

        std::vector<std::string> cleared_fields;
        for(auto const& field: a) {
            if(b.find(field.first) == b.end()) {
                cleared_fields.push_back(field.first);
            }
        }
        return {{ "fields", diff }, { "cleared", cleared_fields }};
    }