我如何将数据添加到蒙戈分贝同步方式? [英] How Do I Add Data To Mongo Db Synchronously?

查看:171
本文介绍了我如何将数据添加到蒙戈分贝同步方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将数据添加到蒙戈分贝同步?它是一个好主意,使用异步方法在服务器添加用户的数据?

我有用户登记表,当创建按钮,它应该添加用户数据蒙戈DB收集用户点击。但在蒙戈DB网站,他们已经解释异步方法来插入使用C#驱动程序的数据。但我想同步插入。
1.用户点击按钮创建
2.将数据蒙戈DB
3.显示成功消息的用户。

在这里第2步,如果是异步的,它会去第3步没有完成第2步是否正确?
因此,用户将在完成请求之前得到成功消息。这不是一个好主意吧?

难道使用异步方法将用户信息了良好的逻辑是什么?

为什么C#蒙戈DB驱动程序只提供异步选项插入\\找到数据?


解决方案

  

这是个好主意,用异步方法在服务器添加用户的数据?


是的,是的,因为它可以让服务器来处理其他请求,而它的等待I / O完成,即你允许相同数量的线程来处理更多的请求。

另外,迫使使用同步。结果 .Wait()是危险的,因为你会的运行到死锁的,如果你不这样做是正确的。


  

难道使用异步方法将用户信息了良好的逻辑是什么?


异步仅意味着该服务器可以使用的线程在其他之间的东西;这在很大程度上是透明的,即你的code没有关心太多。出于实用的目的,只需拨打等待InsertOneAsync(...)或任何你调用MongoDB的驱动程序的方法。


  

为什么C#蒙戈只提供异步选项插入DB驱动程序\\查找数据?


异步是pretty传染性功能:有效地利用异步功能,所有code必须是能够处理它

How do i add data to MOngo Db synchronously ? Is it a good idea to use asynchronously method to add user data in server ?

I have user registration form, when user click on create button it should add user data to Mongo db collection. But in Mongo db site they have explained asynchronous methods to insert data using c# driver. But i want to insert it synchronously. 1. User click on create button 2. Insert data to Mongo DB 3. Show success message to user.

here in step 2, if it is asynchronous, it will go to step3 without completing step 2 correct ? So user will get success message before completing the request. That is not a good idea right ?

Is it a good logic that adding user info using asynchronous method?

Why c# mongo db drivers providing only asynchronous option to insert\find data?

解决方案

Is it a good idea to use asynchronous method to add user data in server?

Yes, it is, since it allows the server to process other requests while it's waiting for I/O to complete, i.e. you allow the same number of threads to handle more requests.

Also, forcing synchrony using .Result or .Wait() is dangerous because you will run into deadlocks if you don't do it right.

Is it a good logic that adding user info using asynchronous method?

asynchronous only means that the server can use the thread for something else in-between; this is largely transparent, i.e. your code doesn't have to care much. For practical purposes, simply call await InsertOneAsync(...) or whatever MongoDB driver method you're calling.

Why c# mongo db drivers providing only asynchronous option to insert\find data?

Async is a pretty 'infectious' feature: to make efficient use of the async feature, all your code must be able to deal with it.

这篇关于我如何将数据添加到蒙戈分贝同步方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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