Java访问MongoDB:安装驱动

Java访问MongoDB:安装驱动。

引用

原帖收藏于IT老兵驿站

前言

Spring项目中可能需要用到MongoDB,要了解一下Java项目如何连接MongoDB,查了查google,对比了一下,发现官网的讲解非常清楚,这样的话,直接阅读官网,效率是最高的。

官网的位置:http://mongodb.github.io/mongo-java-driver/3.9/driver/getting-started/quick-start/。

本文捡着最主要的地方做一下笔记,方便自己学习,也输出一下,这样输出的速度也快,也可以方便他人借鉴,但不再全文翻译,那样输出太慢,而且个人感觉,只是方便了懒人,学习计算机,不提高英文,永远是短一条腿走路,很难达到很高的水平。

Installation
The recommended way to get started using one of the drivers in your project is with a dependency management system.


There are two Maven artifacts available in the release. The preferred artifact for new applications is mongodb-driver-sync however, we still publish the legacy mongo-java-driver uber-jar as well as the mongodb-driver jar introduced in 3.0.

Java的MongoDB驱动当前一共有4个库,先需要搞清楚这4个库的关系,优先推荐mongodb-driver-sync。

MongoDB Driver Sync
The MongoDB Driver mongodb-driver-sync is the synchronous Java driver containing only the generic MongoCollection interface that complies with a new cross-driver CRUD specification. It does not include the legacy API (e.g. DBCollection).

这个库只有MongoCollection接口,并且不包含废弃的API。

IMPORTANT


This is a Java 9-compliant module with an Automatic-Module-Name of org.mongodb.driver.sync.client.


The mongodb-driver-sync artifact is a valid OSGi bundle whose symbolic name is org.mongodb.driver-sync.

这块有点没明白,似乎是分成了两个包,一个是兼容Java 9的org.mongodb.driver.sync.client和遵循OSGi的org.mongodb.driver-sync。

下面是Maven的配置,原帖中还有Gradle的配置方式。

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Note: You can also download the mongodb-driver-sync jar directly from sonatype.


If downloading mongodb-driver-sync manually, you must also download its dependencies: bson and mongodb-driver-core

当然,你可以可以直接从sonatype下载jar包,如果那样的话,你需要还下载一下依赖。

MongoDB Driver Legacy
The MongoDB Legacy driver mongodb-driver-legacy is the legacy synchronous Java driver whose entry point is com.mongodb.MongoClient and central classes include com.mongodb.DB, com.mongodb.DBCollection, and com.mongodb.DBCursor.

这个mongodb-driver-legacy驱动带有com.mongodb.MongoClient这个入口,核心类是com.mongodb.DB, com.mongodb.DBCollection和com.mongodb.DBCursor,很多网上的样例用的是这两个类。

IMPORTANT


While not deprecated, we recommend that new applications depend on the mongodb-driver-syncmodule.

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Note: You can also download the mongodb-driver-legacy jar directly from sonatype.


If downloading mongodb-driver-legacy manually, you must also download its dependencies: bson andmongodb-driver-core

MongoDB Driver
The MongoDB Driver mongodb-driver is the updated synchronous Java driver that includes the legacy API as well as a new generic MongoCollection interface that complies with a new cross-driver CRUD specification.

mongodb-driver是更新的同步化的Java驱动,包含旧的API,也包含新的MongoCollection接口。(这个应该是为了过渡考虑)

IMPORTANT


mongodb-driver is not an OSGi bundle: both mongodb-driver and mongodb-driver-core, a dependency of mongodb-driver, include classes from the com.mongodb package.


For OSGi-based applications, use the mongodb-driver-sync or the mongo-java-driver uber jar instead.


It is also not a Java 9 module.


This module is deprecated and will no longer be published in the next major release of the driver (4.0).

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Note: You can also download the mongodb-driver jar directly from sonatype.


If downloading mongodb-driver manually, you must also download its dependencies: bson and mongodb-driver-core


Uber Jar (Legacy)
For new applications, the preferred artifact is mongodb-driver-sync; however, the legacy mongo-java-driver uber jar is still available. The uber jar contains: the BSON library, the core library, and the mongodb-driver.

uber jar这个包是很老的包,但是还是可以用。

NOTE


This is a Java 9-compliant module with an Automatic-Module-Name of org.mongodb.driver.sync.client.


The mongo-java-driver artifact is a valid OSGi bundle whose symbolic name is org.mongodb.mongo-java-driver.


IMPORTANT


This module is deprecated and will no longer be published in the next major release of the driver (4.0).

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Note: You can also download the mongo-java-driver jar directly from sonatype.

参考

http://mongodb.github.io/mongo-java-driver/3.9/driver/getting-started/installation/