知识库 : MongoDB3.0的Java Driver介绍

Edit Document

 

 

 

 

 

 

 

 

 

MongoDB 3.0 的 Java Driver 介绍

 

 

 


目录

1.               驱动下载

2.               使用介绍

2.1.               获取连接

2.2.               获取一个集合

2.3.               插入一条文档

2.4.               查询所有文档

2.5.               查询单一文档

2.6.               更新文档

2.7.               删除

3.               API 文档

 

1.     驱动 下载

从 http://central.maven.org/maven2/org/mongodb/mongo-java-driver/ 选择一个版本进行下载,目前最新的驱动版本是 3.0.2 ,具体下载以下 jar 包:

mongo-java-driver-3.0.2.jar  

mongo-java-driver-3.0.2-sources.jar

 

2.     使用 介绍

使用 MongoDB 的 Java 驱动非常简单,只要确保将 mongo-java-driver-3.0.2.jar 文件添加到 classpath 中即可。

2.1.     获取连接

使用类 com.mongodb.MongoClient 来链接运行有 mongod 进程的 MongoDB 实例,并通过   com.mongodb.client.MongoDatabase   接口来访问 MongoDB 数据库。访问的过程中需要指定要连接的数据库如果库不存在, MongoDB 将创建一个新的库。另外,在连接时需要指定服务器地址和端口。下面是使用不同方式连接本地机器的 UFS 数据库的方式:

MongoClient mongoClient = new MongoClient();

MongoClient mongoClient = new MongoClient( "localhost" );

MongoClient mongoClient = new MongoClient( "localhost" , 27017);

MongoClient mongoClient = new MongoClient(Arrays. asList ( new ServerAddress( "localhost" , 27017)));

MongoDatabase db = mongoClient .getDatabase( "UFS" );

2.2.     获取一个集合

获取一个集合,只需要指定集合名称调用 getCollection(Sring collectionName) 方法:

MongoCollection<Document> collection = db .getCollection( "default" );

一旦取得集合对象,就可以对其进行查询,插入数据等操作。

 

2.3.     插入一条文档

一旦取得集合对象,就可以在集合中插入文档。例如,一个 json 格式表示的文档 ( 下面 文档中有一个内嵌文档 ) :

{ file " : " FileInputStream ", "type" : " FileType ", " version " : 1, " extInfo " : {" size ": 122 , " module ": " default "}}

使用 Document 类来创建文档 ( 包括内部文档 ) ,然后调用集合的 insertOne() 方法插入文档:

db .getCollection( "default" ).insertOne(

                new Document( "file" , "FileInputStream" ).append( "type" , "FileType" ).append( "version" , 1)

                        .append( "extInfo" , new Document( "size" , 122).append( "module" , "default" )));

 

2.4.     查询 所有文档

调用不带有参数的 find() 方法可以返回一个集合中的所有文档,例如以下的查询返回的是集合 default 中的所有文档:

FindIterable<Document> iterable = db .getCollection( "default" ).find();

find() 返回的是 FindIterable 对象,对该对象遍历可输出所有的文档信息:

iterable .forEach( new Block<Document>() {

            @Override

            public void apply( final Document document ) {

                System. out .println( document );

            }

        });

 

2.5.     查询单一 文档

可以通过给 find() 方法传递一个参数来查询集合的部分文档。例如,查出文档的 "_id" 字段的值为 "556aa95ba990f0060cea28a4" 的文档:

FindIterable<Document> iterable =

             db .getCollection( "default" ).find( new Document( "_id" , "556aa95ba990f0060cea28a4" ));

iterable .forEach( new Block<Document>() {

            @Override

            public void apply( final Document document ) {

                System. out .println( document );

            }

        });

以上 的查询 结果 将打印出一条文档:

Document{{_id=556aa95ba990f0060cea28a4, content=org.bson.types.Binary@d4c9f081} }

也可以 使用 Filters 指定查询 的条件, 如 :

FindIterable<Document> iterable = db .getCollection( "default" ).find( eq ( "_id" , "556aa95ba990f0060cea28a4" ));

 

2.6.     更新 文档

为了修改 文档中的 key 对对应 的 value 值, MongoDB 提供 了 update 操作 , 如 使用 $set 来 修改 values 。使用 $set 更新操作 时,如果 key 不存在,则会新增 key, value 值 。

以下 的 更新操作是 更新 _id 值 为 ” 556aa95ba990f0060cea28a4” 所对应 的文档中的 content 值 。

db.getCollection( "default" ).updateOne( new Document( "_id" , "556aa95ba990f0060cea28a4" ),

            new Document( "$set" , new Document( "content" , "East 31st Street" )));

updateOne 操作 会返回包含操作信息的 UpdateResult 对象 , 调用 该对象中的 get ModifiedCpunt() 方法 可以得到修改的文档数目。

 

2.7.     删除

删除 所有 content 值 为 “Manhattan” 的 文档:

        db .getCollection( "default" ).deleteMany( new Document( "content" , "Manhattan" ));

deleteMany 会返回 DeleteResult 对象 , 使用 getDeleted Count() 方法 可以获得删除 文档 的数目。

要 删除一个集合中的所有文档,可以 给 deleteMany() 传递一个 不带有参数的 document {}

        db .getCollection( "default" ).deleteMany( new Document());

删除 的操作 只会 从集合中 删除文档 , 集合 本身 包括 集合创建的索引 还 保留 。 删除 整个 集合要比删除集合中的所有文档 更高效 , 使用 drop() 方法 可以删除一个集合,包括 其中 的任何索引。

        db .getCollection( "default" ).drop();

3.     API 文档

相关 的 API 文档可以访问: http://api.mongodb.org/java/3.0/

 

Attachments:

MongoDB.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)
MongoDB.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)