我想在 Linux 中使用 C++ 进程设置 TTL 索引。
但我发现 ensureIndex
已被删除。 ( https://github.com/mongodb/mongo-cxx-driver/pull/106 )
createIndex
的参数似乎只有BSONObj
可以输入。
我试过:
mongo::DBClientConnection mConnection;
mConnection.connect("localhost");
mongo::BSONObj bObj = BSON( "mongo_date"<< 1 << "expireAfterSeconds" << 10);
mConnection.createIndex("Test.Data",bObj)
但结果是:
db.system.indexes.find()
{ "v": 1, "key": { "_id": 1 }, "name": "_id_", "ns": "Test.Data"}
{ "v": 1, "key": { "mongo_date": 1, "expireAfterSeconds": 10 }, "name": "mongo_date_1_expireAfterSeconds_10", "ns": "Test.Data"}
TTL设置有问题还是有其他方法?
谢谢。
最佳答案
因为我还是找不到C中的方法,所以临时用了一个笨方法。
我使用 shell 脚本来创建和运行 JavaScript
在 C 代码中:
int expire = 321;
char expir_char[20];
sprintf(expir_char, "%d",expire);
char temp_char[30] = "./runTtlJs.sh ";
strcat(temp_char,expir_char);
system(temp_char);
在 runTtlJs.sh 中:
echo "db.Data.dropIndex({"mongo_date":1})" > ttl.js
echo "db.Data.ensureIndex({"mongo_date":1}, { expireAfterSeconds: $1 })" >> ttl.js
mongo Test ttl.js
我知道这真的不是一个好的答案。
关于c++ - 如何使用 C++ 驱动程序在 mongodb 中设置 TTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793513/