0

MongoDB C ライブラリを使用してドキュメントを同じデータベース内のさまざまなコレクションに挿入しています。

コレクション全体で同じ oid を使用したかっので、各コレクションのタイムスタンプ付きの各エントリが同じ oid を持つようにしましたが、そのときにエラーが発生し始めました。だから私はそれを取りやめ、各エントリに新しい OID を作成しようとしましたが、それでも同じエラーが発生します。

OID を再利用しようとするバージョン 1:

int insert_mongo(char json[100], char *coll, mongoc_client_t *client, bson_oid_t oid){

    mongoc_collection_t *collection;
    bson_error_t error;
    bson_t *doc;

    collection = mongoc_client_get_collection (client, "edison", coll);     
    doc = bson_new_from_json((const uint8_t *)json, -1, &error);
    BSON_APPEND_OID (doc, "_id", &oid);
    if (!doc) {
        fprintf (stderr, "%s\n", error.message);
        return EXIT_FAILURE;
    }

    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
        return EXIT_FAILURE;
    }
    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    return EXIT_SUCCESS;
}

新しい OID を作成するバージョン 2:

int insert_mongo(char json[100], char *coll, mongoc_client_t *client){

    mongoc_collection_t *collection;
    bson_error_t error;
    bson_t *doc;
    bson_oid_t oid;

    bson_oid_init (&oid, NULL);
    collection = mongoc_client_get_collection (client, "edison", coll);
    doc = bson_new_from_json((const uint8_t *)json, -1, &error);
    BSON_APPEND_OID (doc, "_id", &oid);
    if (!doc) {
        fprintf (stderr, "%s\n", error.message);
        return EXIT_FAILURE;
    }

    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
        return EXIT_FAILURE;
    }
    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    return EXIT_SUCCESS;
}

両方のバージョンで、MongoDB bson_append_oid(): precondition failed: bson で関数が呼び出された 2 回目にセグ フォールトが発生します。

4

1 に答える 1