如何使用C代码在mongodb中创建数组中的子数组?

时间:2017-03-30 11:31:33

标签: c arrays mongodb bson sub-array

我正在进行项目,我必须预先填充数组。我需要更新这个函数,以便数组' time'和#'价值观'成为阵列,其子阵列充满了' 0'但我无法弄清楚我是怎么想这样做的。 我会感激任何帮助!

int prepopulate(mongoc_collection_t *collection, int nvalues) {
    bson_t *doc, *values, *time;
    bson_error_t error;
    bson_oid_t oid;
    doc = bson_new ();
    values = bson_new ();
    time = bson_new();
    bson_oid_init (&oid, NULL);
    BSON_APPEND_OID (doc, "_id", &oid);
    BSON_APPEND_INT64 (doc, "series", 0);
    BSON_APPEND_INT64 (doc, "prevEnd", 0);
    BSON_APPEND_INT64 (doc, "nextStart", 0);
    int i;
    for (i=0; i<nvalues; i++) {
        BSON_APPEND_INT64(values, "0", 0);
        BSON_APPEND_INT64(time, "0", 0);
    }
    bson_append_array (doc, "value", -1, values);
    bson_append_array (doc, "time", -1, time);

    int res = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, 
    NULL, &error);

    bson_destroy (doc);
    bson_destroy (values);
    bson_destroy (time);
    return res;
}

0 个答案:

没有答案