[英]MongoDB Java Driver: MongoCore Driver vs. MongoDB Driver vs. MongoDB Async Driver
There are three different driver options for the MongoDB Java driver: MongoDB Java驱动程序有三种不同的驱动程序选项:
The drivers description page gives a brief description of each of them but no further explanation is provided regarding when I should use each of them. 驱动程序描述页面给出了每个驱动程序描述页面的简要说明,但没有提供关于何时应该使用它们的进一步解释。 My question: can you, please, clarify what are the cases to use each of them? 我的问题:请你澄清一下使用它们的案例是什么? When should I prefer one over the second and when I must/have to use the particular driver option? 什么时候我应该更喜欢一个,当我必须/必须使用特定的驱动程序选项?
TL;DR : TL; DR :
Use the async driver if the operations are slow, or use the regular driver in most cases. 如果操作很慢,请使用异步驱动程序,或者在大多数情况下使用常规驱动程序。 You shouldn't use the core driver. 您不应该使用核心驱动程序。
MongoDB Regular Driver : MongoDB常规驱动程序 :
General driver that you can use to search, create, read, update and delete documents. 可用于搜索,创建,读取,更新和删除文档的常规驱动程序。 The find(...)
, updateMany(...)
, deleteMany(...)
and similar methods will hang for as long as the result is not returned or the operation not done (synchronous behavior). 只要未返回结果或未执行操作(同步行为), find(...)
, updateMany(...)
, deleteMany(...)
和类似方法将挂起。 This is the driver that most program uses and is good in most cases. 这是大多数程序使用的驱动程序,并且在大多数情况下都很好。
Here is an example for inserting a single Document: 以下是插入单个文档的示例:
collection.insertOne(doc);
//Do something here.
System.out.println("Inserted!")
MongoDB Async Driver : MongoDB异步驱动程序 :
Another type of driver that you can use to search, create, read, update and delete documents. 您可以使用另一种类型的驱动程序来搜索,创建,读取,更新和删除文档。 This driver offers similar methods than the regular driver ( find(...)
, updateMany(...)
, deleteMany(...)
, etc.). 此驱动程序提供与常规驱动程序类似的方法( find(...)
, updateMany(...)
, deleteMany(...)
等)。
The difference with the regular driver is that the main thread will not hang because the async driver sends the result in a callback (asynchronous behavior). 与常规驱动程序的区别在于主线程不会挂起,因为异步驱动程序在回调中发送结果(异步行为)。 This driver is used when the operations can take a long time (a lot of data to go through, high latency, query on unindexed fields, etc.) and you do not want to manage multiple threads. 当操作可能需要很长时间(大量数据要经过,高延迟,在未编制索引的字段上查询等)并且您不想管理多个线程时,将使用此驱动程序。
Here is an example of the callback when inserting a single Document: 以下是插入单个Document时回调的示例:
collection.insertOne(doc, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
//Do something here.
System.out.println("Inserted!");
}
});
// Do something to show that the Document was not inserted yet.
System.out.println("Inserting...")
For more informations, read this . 有关更多信息,请阅读此内容 。
MongoDB Core Driver MongoDB核心驱动程序
Base layer of the regular and async drivers. 常规和异步驱动程序的基础层。 It contains low-level methods to do all the operations common to the regular and async drivers. 它包含执行常规和异步驱动程序通用的所有操作的低级方法。 Unless you are making a new API / Driver for MongoDB, you shouldn't use the core driver. 除非您为MongoDB创建新的API /驱动程序,否则不应使用核心驱动程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.