MongoDB Java驱动程序:MongoCore驱动程序与MongoDB驱动程序与MongoDB异步驱动程序 [英] MongoDB Java Driver: MongoCore Driver vs. MongoDB Driver vs. MongoDB Async Driver

查看:150
本文介绍了MongoDB Java驱动程序:MongoCore驱动程序与MongoDB驱动程序与MongoDB异步驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB Java驱动程序有三种不同的驱动程序选项:

There are three different driver options for the MongoDB Java driver:

  1. 核心驱动程序
  2. MongoDB驱动程序
  3. MongoDB异步驱动程序

驱动程序描述页面给出了每个驱动程序的简短描述,但没有有关何时应使用它们的详细说明.我的问题:能否请您说明分别使用哪种情况?什么时候我应该比第二个更喜欢?何时/必须使用特定的驱动程序选项?

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 :

如果操作缓慢,请使用异步驱动程序;在大多数情况下,请使用常规驱动程序.您不应该使用核心驱动程序.

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常规驱动程序:

可用于搜索,创建,读取,更新和删除文档的通用驱动程序.只要不返回结果或未完成操作(同步行为),find(...)updateMany(...)deleteMany(...)和类似方法将挂起.这是大多数程序使用的驱动程序,在大多数情况下都很好.

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). 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异步驱动程序:

另一种类型的驱动程序,可用于搜索,创建,读取,更新和删除文档.该驱动程序提供与常规驱动程序类似的方法(find(...)updateMany(...)deleteMany(...)等).

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.).

与常规驱动程序的区别在于主线程不会挂起,因为异步驱动程序将结果发送到

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:

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驱动程序

常规和异步驱动程序的基础层.它包含执行常规驱动程序和异步驱动程序通用的所有操作的低级方法.除非您为MongoDB创建新的API/驱动程序,否则不应该使用核心驱动程序.

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 Java驱动程序:MongoCore驱动程序与MongoDB驱动程序与MongoDB异步驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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