如何使用 Mongoc (mongo c lib) 创建 TTL 索引

How to create a TTL index with Mongoc (mongo c lib)

我正在尝试使用 mongoc 库 http://mongoc.org/ 在我的 mongodb 中添加 TTL 索引。

我正在做的是:

   keys = BCON_NEW("createdAt", BCON_INT32(1), "expireAfterSeconds", BCON_INT32(30));

   index_name = mongoc_collection_keys_to_index_string(keys);

   create_indexes = BCON_NEW("createIndexes", BCON_UTF8(COLLECTION_TEST), "indexes", "[", "{", "key", BCON_DOCUMENT(keys), "name", BCON_UTF8(index_name), "}", "]");

   mongoc_database_write_command_with_opts (database, create_indexes, NULL /* opts */, NULL, &error); 

问题是这段代码生成了一个 json 这种格式:

{ "createdAt": 1 }, { "expireAfterSeconds": 3600 }

我正在寻找格式为 json 的

{ "createdAt": 1 }, { expireAfterSeconds: 3600 } // The expireAfterSeconds must not be inside a "".

我想通了:

keys = BCON_NEW("expireAt", BCON_INT32(1));

create_indexes = BCON_NEW ("createIndexes", BCON_UTF8 (COLLECTION_TEST), "indexes", "[", "{", "key", BCON_DOCUMENT (keys[0]), "expireAfterSeconds", BCON_INT32(0), "name", BCON_UTF8 (index_name[0]), "}", "]");