插入使用InsertOneAsync新的文档(.NET 2.0驱动程序) [英] Insert new document using InsertOneAsync (.NET Driver 2.0)

查看:777
本文介绍了插入使用InsertOneAsync新的文档(.NET 2.0驱动程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了.NET API版本:

  MongoClient客户端=新MongoClient(); 
VAR服务器= client.GetServer();
变种DB = server.GetDatabase(富);
变种集合= db.GetCollection< BsonDocument>(巴);
变种文档=新BsonDocument {{_id,1},{×,2}};
collection.Save(文件);



它的工作。



当我使用新的.NET驱动程序2.0:

  VAR的客户=新MongoClient(mongodb的://本地主机:27017); 
VAR数据库= client.GetDatabase(富);
变种集合= database.GetCollection< BsonDocument>(巴);

变种文档=新BsonDocument {{_id,1},{×,2}};
等待collection.InsertOneAsync(文件);




错误:'等待'运算符只能异步内使用方法。
考虑标志着该方法与异步修饰和改变
返回类型为'任务'。




参考文献:



介绍.NET 2.0驱动程序



阅读与写作



我想问问如何使用的.Net 2.0驱动程序中插入一个新的文档。谢谢



[更新1]我试图执行:

 公共类资源库
{
公共静态异步任务插入()
{
VAR的客户=新MongoClient(mongodb的://本地主机:27017);
VAR数据库= client.GetDatabase(富);
变种集合= database.GetCollection< BsonDocument>(巴);

变种文档=新BsonDocument {{_id,1},{×,2}};
等待collection.InsertOneAsync(文件);
}
}

静态无效的主要(字串[] args)
{
任务TSK = Repository.Insert();
tsk.Wait();
Console.WriteLine(状态:+ tsk.Status);
}



结果:WaitingForActivati​​on。没有在数据库中更改。请帮帮我!



[更新2(解决)]:添加tsk.Wait();有效 !
感谢这篇文章:我将如何同步运行异步任务的方法


解决方案

您的方法应该是这样的。

 公共异步无效插入()
{
VAR的客户=新MongoClient(mongodb的://本地主机:27017);
VAR数据库= client.GetDatabase(富);
变种集合= database.GetCollection< BsonDocument>(巴);

变种文档=新BsonDocument {{_id,1},{×,2}};
等待collection.InsertOneAsync(文件);

}


In order .Net API version :

MongoClient client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("foo");
var collection = db.GetCollection<BsonDocument>("bar");
var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
collection.Save(document);

It worked.

When i use new .Net Driver 2.0 :

var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("foo");
var collection = database.GetCollection<BsonDocument>("bar");

var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
await collection.InsertOneAsync(document);

Error : The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

Refs :

Introducing the 2.0 .NET Driver

Reading and Writing

I want to ask how to insert a new document using .Net Driver 2.0. Thanks.

[Update 1] I tried to implement :

public class Repository
{
    public static async Task Insert()
    {
        var client = new MongoClient("mongodb://localhost:27017");
        var database = client.GetDatabase("foo");
        var collection = database.GetCollection<BsonDocument>("bar");

        var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
        await collection.InsertOneAsync(document);
    }
}

static void Main(string[] args)
{            
       Task tsk = Repository.Insert();
       tsk.Wait();
       Console.WriteLine("State: " + tsk.Status);            
}

Result : WaitingForActivation. Nothing changed in database. Please help me!

[Update 2 (Solved)] : add tsk.Wait(); It worked ! Thanks this post : How would I run an async Task method synchronously?

解决方案

Your method should be like

 public async void Insert()
    {
         var client = new MongoClient("mongodb://localhost:27017");
        var database = client.GetDatabase("foo");
        var collection = database.GetCollection<BsonDocument>("bar");

        var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
        await collection.InsertOneAsync(document);

    }

这篇关于插入使用InsertOneAsync新的文档(.NET 2.0驱动程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆