使用C#API创建以mongodb为上限的集合 [英] Creating a mongodb capped collection using c# api
问题描述
使用C#MongoDB驱动程序,我们当前如下创建集合:
Using the C# MongoDB driver, we currently create our collection like so:
MongoServer mongoServer = MongoServer.Create("some conn str");
MongoDatabase db = mongoServer.GetDatabase("mydb");
MongoCollection logs = db.GetCollection("mycoll");
我想将mycoll
用作上限集合.我还没有看到有关如何使用C#驱动程序创建上限集合的任何示例或文档说明.我发现了大量的JS示例,甚至Java示例(在这里:创建Java中的mongodb封顶的集合).
I would like to use mycoll
as a capped collection. I haven't seen any examples or documentation specifics on how to create a capped collection using the C# driver. I've found tons of JS examples, and even a Java example (here: Creating a mongodb capped collection in java).
是否有人曾经做过此事,或者是否知道在C#中是可能的?
Has anyone had to do this before, or know if it's possible in C#?
推荐答案
创建集合时,需要指定使用CollectionOptions
设置集合的上限:
When creating a collection, you need to specify that the collection should be capped by using CollectionOptions
:
CollectionOptionsBuilder options = CollectionOptions.SetCapped(true);
database.CreateCollection("mycoll", options);
您需要显式创建集合(通过调用CreateCollection
方法),以便能够提供您的选项.当使用不存在的集合调用GetCollection
时,它将使用默认选项隐式创建.
You need to create the collection explicitly (by calling CreateCollection
method) to be able to provide your options. When calling GetCollection
with non-existing collection, it gets implicitly created with default options.
这篇关于使用C#API创建以mongodb为上限的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!