简体   繁体   English

如何使用C ++驱动程序在mongodb中设置TTL

[英]How to set TTL in mongodb with C++ driver

I want to set TTL index with C++ process in Linux. 我想在Linux中使用C ++进程设置TTL索引。

But I've found the ensureIndex is removed. 但是我发现ensureIndex被删除了。 ( https://github.com/mongodb/mongo-cxx-driver/pull/106 ) https://github.com/mongodb/mongo-cxx-driver/pull/106

The argument of createIndex seems only BSONObj can input. createIndex的参数似乎只有BSONObj可以输入。

I've tried: 我试过了:

mongo::DBClientConnection mConnection;
mConnection.connect("localhost");
mongo::BSONObj bObj = BSON( "mongo_date"<< 1 << "expireAfterSeconds" << 10);
mConnection.createIndex("Test.Data",bObj)

but the result is: 但结果是:

db.system.indexes.find() 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" }

Is there something wrong or other way to set the TTL? 设置TTL是否有错误或其他方法?

Thanks. 谢谢。

Because I still can't find the method in C, so I use a stupid method temporarily. 因为我仍然无法在C中找到该方法,所以我暂时使用了一个愚蠢的方法。

I use shell script to create and run a JavaScript 我使用Shell脚本来创建和运行JavaScript

In C code: 在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);

In runTtlJs.sh: 在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

I know it's really not a good answer. 我知道这确实不是一个好答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM