我的代码有一个例外: 要测试tailable游标,请在此程序运行时手动将文档插入test.capped集合,并验证它们是否已回显到控制台窗口。
public static class Program
{
public static void Main(string[] args)
{
try
{
var server = MongoServer.Create("mongodb://distnatserver/?safe=true");
var database = server["test"];
if (database.CollectionExists("capped"))
{
database.DropCollection("capped");
}
var collectionOptions = CollectionOptions.SetCapped(true).SetMaxDocuments(5);
var commandResult = database.CreateCollection("capped", collectionOptions);
var collection = database["capped"];
BsonValue lastId = BsonMinKey.Value;
while (true)
{
var query = Query.GT("_id", lastId);
var cursor = collection.Find(query)
.SetFlags(QueryFlags.TailableCursor | QueryFlags.AwaitData)
.SetSortOrder("$natural");
using (var enumerator = (MongoCursorEnumerator<BsonDocument>)cursor.GetEnumerator())
{
while (true)
{
if (enumerator.MoveNext())
{
var document = enumerator.Current;
lastId = document["_id"];
ProcessDocument(document);
}
else {
if (enumerator.IsDead)
{
break;
}
if (!enumerator.IsServerAwaitCapable)
{
Thread.Sleep(TimeSpan.FromMilliseconds(100));
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Unhandled exception:");
Console.WriteLine(ex);
}
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
}
private static void ProcessDocument(
BsonDocument document
)
{
Console.WriteLine(document.ToJson());
}
}
未处理的异常: MongoDB.Driver.MongoCommandException:Command&#39; create&#39;失败:指定大小:当上限为真时(响应:{&#34; ok&#34;:0.0,&#34; errmsg&#34;:&#34;指定大小:当上限为真时&#34;,&# 34;代码&#34;:14832})
答案 0 :(得分:1)
根据documentation,在创建上限集合时,您必须以字节为单位指定集合的最大大小,例如:
db.createCollection( "log", { capped: true, size: 100000 } )