如何使用Mongoc(mongo-clib)创建TTL索引



我正试图用mongoc-lib在我的mongodb中添加一个TTL索引http://mongoc.org/.

我正在做的是:

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]), "}", "]");

最新更新