C# C中MongoDB的异步插入#
如何在C#中异步插入/更新MongoDB? 懒惰持久性的术语是什么C# C中MongoDB的异步插入#,c#,mongodb,insert,async-await,C#,Mongodb,Insert,Async Await,如何在C#中异步插入/更新MongoDB? 懒惰持久性的术语是什么 写在后面 默认情况下,MongoDB插入是异步的,因为它是fire and forget。错误检查是一个显式操作,或者您必须在驱动程序级别启用安全模式。 如果您需要真正的异步操作:使用消息队列。MongoDB插入在默认情况下是异步的,因为它是fire and forget。错误检查是一个显式操作,或者您必须在驱动程序级别启用安全模式。 如果您需要真正的异步操作:请使用消息队列。在缓存世界中,“惰性持久性”将被称为写后操作。看看
- 写在后面
如果您需要真正的异步操作:使用消息队列。MongoDB插入在默认情况下是异步的,因为它是fire and forget。错误检查是一个显式操作,或者您必须在驱动程序级别启用安全模式。
如果您需要真正的异步操作:请使用消息队列。在缓存世界中,“惰性持久性”将被称为写后操作。看看这个: 可能最简单的方法是使用C#异步方法调用。这将告诉您如何: 代码将类似于:
- 定义您自己的代理:
private delegate void InsertDelegate(BsonDocument doc);
- 使用它
MongoCollection<BsonDocument> books = database.GetCollection<BsonDocument>("books"); BsonDocument book = new BsonDocument { { "author", "Ernest Hemingway" }, { "title", "For Whom the Bell Tolls" } }; var insert = new InsertDelegate(books.Insert); // invoke the method asynchronously IAsyncResult result = insert.BeginInvoke(book, null, null); // DO YOUR OWN WORK HERE // get the result of that asynchronous operation insert.EndInvoke(result);
MongoCollection books=database.GetCollection(“books”); b附属文件簿=新的附属文件{ {“作者”,“欧内斯特·海明威”}, {“头衔”,“钟声为谁而鸣”} }; var insert=新的InsertDelegate(books.insert); //异步调用该方法 IAsyncResult result=insert.BeginInvoke(book,null,null); //在这里做你自己的工作 //获取异步操作的结果 insert.EndInvoke(结果);
希望这能有所帮助。在缓存世界中,“惰性持久性”将被称为“写在后面”。看看这个: 可能最简单的方法是使用C#异步方法调用。这将告诉您如何: 代码将类似于:
- 定义您自己的代理:
private delegate void InsertDelegate(BsonDocument doc);
- 使用它
MongoCollection<BsonDocument> books = database.GetCollection<BsonDocument>("books"); BsonDocument book = new BsonDocument { { "author", "Ernest Hemingway" }, { "title", "For Whom the Bell Tolls" } }; var insert = new InsertDelegate(books.Insert); // invoke the method asynchronously IAsyncResult result = insert.BeginInvoke(book, null, null); // DO YOUR OWN WORK HERE // get the result of that asynchronous operation insert.EndInvoke(result);
MongoCollection books=database.GetCollection(“books”); b附属文件簿=新的附属文件{ {“作者”,“欧内斯特·海明威”}, {“头衔”,“钟声为谁而鸣”} }; var insert=新的InsertDelegate(books.insert); //异步调用该方法 IAsyncResult result=insert.BeginInvoke(book,null,null); //在这里做你自己的工作 //获取异步操作的结果 insert.EndInvoke(结果);