Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MongoDB.Driver 2在Azure DocumentDB中设置生存时间(TTL)_C#_Mongodb_Azure Cosmosdb_Ttl_Mongodb.driver - Fatal编程技术网

C# 使用MongoDB.Driver 2在Azure DocumentDB中设置生存时间(TTL)

C# 使用MongoDB.Driver 2在Azure DocumentDB中设置生存时间(TTL),c#,mongodb,azure-cosmosdb,ttl,mongodb.driver,C#,Mongodb,Azure Cosmosdb,Ttl,Mongodb.driver,我正在尝试使用MongoDB.Driver在DocumentDB中实现TTL。我创建了一个索引,如 await collection.Indexes.CreateOneAsync ( Builders<T>.IndexKeys.Ascending("_id123"), new CreateIndexOptions { ExpireAfter = new TimeSpan(0, 0, 10) } ); wait collection.index.CreateOneAs

我正在尝试使用MongoDB.Driver在DocumentDB中实现TTL。我创建了一个索引,如

await collection.Indexes.CreateOneAsync
(
    Builders<T>.IndexKeys.Ascending("_id123"),
    new CreateIndexOptions { ExpireAfter = new TimeSpan(0, 0, 10) }
);
wait collection.index.CreateOneAsync
(
Builders.IndexKeys.Ascending(“_id123”),
新建CreateIndexOptions{ExpireAfter=new TimeSpan(0,0,10)}
);

上面的代码正在创建_id123索引,没有任何错误,但是插入的数据在10秒后不会过期。请帮助我解决此问题。

根据我的经验,请尝试使用
\t字段。我们可以从网站上获得更多信息。
我在我这边做了一个创建TTL索引的演示,它工作正常。以下是详细步骤

1.创建C#控制台项目并添加

2.添加一个人类

using MongoDB.Bson;
public class Person
        {
            public ObjectId Id { get; set; }
            public string Name { get; set; }
        }
3.创建MongodB客户端,我们可以从Azure门户获取代码

4.将文档添加到集合中

 var db = mongoClient.GetDatabase("dbname");

 var collection = db.GetCollection<Person>("collectionname");

 collection.InsertOne(new Person() {Name = "tom"});

Package.config文件

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MongoDB.Bson" version="2.4.3" targetFramework="net451" />
  <package id="MongoDB.Driver" version="2.4.3" targetFramework="net451" />
  <package id="MongoDB.Driver.Core" version="2.4.3" targetFramework="net451" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net451" />
</packages>

我找到的一个更简单的方法是:

  • 转到数据库>配置>预览功能>启用每个文档TTL

  • 转到DB data explorer中的“缩放和设置”集合,并设置对象的生存时间:

  • 在DB中插入每个对象时,可以为它们指定TTL(这将覆盖以前建立的集合TTL)。您只需在文档中添加一个名为“ttl”的字段,它是一个
    double
    ,格式为:
    ttl=3600.0


  • 就这样,文档将在一小时后自行删除。祝你好运

    “不工作”的意思是。。。什么?是否有错误消息?什么都没有发生吗?嗨,大卫,代码没有返回任何错误,但插入的文档在10秒后不会过期。如果有用,请将其标记为有助于更多社区的答案。谢谢Tom Sun。在我的例子中,索引已成功创建,但在azure DocumentDB中插入的数据在10秒后不会过期。在您的测试项目中,插入的数据是否在10秒后过期?当我设置在10秒后过期时,在文档从集合中删除之后,它在我这边正常工作。请尝试使用例如100秒进行测试。在我的选项中,10sec是test的缩写。TTL特性由两个级别的TTL属性控制——收集级别和文档级别。这些值以秒为单位设置,并被视为来自_ts的增量。请使用
    \u ts
    字段。更多信息请参阅。我已经更新了我的回复。这个选项已经过时了,我已经添加了一种新的方式来完成它,这对我来说很有效。
    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="MongoDB.Bson" version="2.4.3" targetFramework="net451" />
      <package id="MongoDB.Driver" version="2.4.3" targetFramework="net451" />
      <package id="MongoDB.Driver.Core" version="2.4.3" targetFramework="net451" />
      <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net451" />
    </packages>