在本章中,无涯教程将学习如何设置MongoDB JDBC驱动程序。
安装驱动
在Java程序中开始使用MongoDB之前,需要确保在计算机上设置了MongoDB JDBC驱动程序和Java,您可以检查Java教程以在计算机上安装Java。现在,让无涯教程检查如何设置MongoDB JDBC驱动程序。
您需要从路径下载 jar mongo.jar。确保下载它的最新版本。
您需要将mongo.jar包含到您的类路径中。
连接数据库
要连接数据库,您需要指定数据库名称,如果数据库不存在,则MongoDB会自动创建它。
以下是连接到数据库的代码片段-
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class ConnectToDB { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); System.out.println("Credentials ::"+ credential); } }
现在,让无涯教程编译并运行上述程序以创建无涯教程的数据库myDb,如下所示。
$javac ConnectToDB.java $java ConnectToDB
执行后,上述程序将为您提供以下输出。
Connected to the database successfully Credentials ::MongoCredential{ mechanism=null, userName='sampleUser', source='myDb', password=<hidden>, mechanismProperties={} }
创建集合
要创建集合,请使用 com.mongodb.client.MongoDatabase 类的 createCollection()方法。
以下是创建集合的代码片段-
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class CreatingCollection { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //创建集合 database.createCollection("sampleCollection"); System.out.println("Collection created successfully"); } }
在编译时,上述程序为您提供以下输出-
Connected to the database successfully Collection created successfully
获取/选择集合
要从数据库中获取/选择集合,请使用 com.mongodb.client.MongoDatabase 类的 getCollection()方法。
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class selectingCollection { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //创建集合 System.out.println("Collection created successfully"); //检索集合 MongoCollection<Document> collection = database.getCollection("myCollection"); System.out.println("Collection myCollection selected successfully"); } }
在编译时,上述程序为您提供以下输出-
Connected to the database successfully Collection created successfully Collection myCollection selected successfully
插入数据
要将文档插入MongoDB,请使用 com.mongodb.client.MongoCollection 类的 insert()方法。
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class InsertingDocument { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //检索集合 MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection sampleCollection selected successfully"); Document document = new Document("title", "MongoDB") .append("id", 1) .append("description", "database") .append("likes", 100) .append("url", "http://www.learnfk.com/mongodb/") .append("by", "Learnfk point"); collection.insertOne(document); System.out.println("Document inserted successfully"); } }
在编译时,上述程序为您提供以下输出-
Connected to the database successfully Collection sampleCollection selected successfully Document inserted successfully
检索数据
要从集合中选择所有文档,请使用 com.mongodb.client.MongoCollection 类的 find()方法,此方法返回一个游标,因此您需要迭代此游标。
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.Iterator; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class RetrievingAllDocuments { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //检索集合 MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection sampleCollection selected successfully"); //获取可迭代对象 FindIterable<Document> iterDoc = collection.find(); int i = 1; //获取迭代器 Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } }
在编译时,上述程序为您提供以下输出-
Document{{ _id=5967745223993a32646baab8, title=MongoDB, id=1, description=database, likes=100, url=http://www.learnfk.com/mongodb/, by=Learnfk point }} Document{{ _id=7452239959673a32646baab8, title=RethinkDB, id=2, description=database, likes=200, url=http://www.learnfk.com/rethinkdb/, by=Learnfk point }}
更新数据
要从集合中更新文档,请使用 com.mongodb.client.MongoCollection 类的 updateOne()方法。
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Updates; import java.util.Iterator; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class UpdatingDocuments { public static void main( String args[] ) { //获取迭代器 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建 Mongo 客户端 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //检索集合 MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection myCollection selected successfully"); collection.updateOne(Filters.eq("id", 1), Updates.set("likes", 150)); System.out.println("Document update successfully..."); //更新后检索文件 //获取可迭代对象 FindIterable<Document> iterDoc = collection.find(); int i = 1; //获取迭代器 Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } }
在编译时,上述程序为您提供以下输出-
Document update successfully... Document {{ _id=5967745223993a32646baab8, title=MongoDB, id=1, description=database, likes=150, url=http://www.learnfk.com/mongodb/, by=Learnfk point }}
删除数据
要从集合中删除文档,您需要使用 com.mongodb.client.MongoCollection 类的 deleteOne()方法。
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import java.util.Iterator; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class DeletingDocuments { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //检索集合 MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection sampleCollection selected successfully"); //删除文件 collection.deleteOne(Filters.eq("id", 1)); System.out.println("Document deleted successfully..."); //更新后检索文件 //获取可迭代对象 FindIterable<Document> iterDoc = collection.find(); int i = 1; //获取迭代器 Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println("Inserted Document: "+i); System.out.println(it.next()); i++; } } }
在编译时,上述程序为您提供以下输出-
Connected to the database successfully Collection sampleCollection selected successfully Document deleted successfully...
删除集合
要从数据库中删除集合,您需要使用 com.mongodb.client.MongoCollection 类的 drop()方法。
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class DropingCollection { public static void main( String args[] ) { //创建 Mongo 客户端 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建凭证 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); //创建集合 System.out.println("Collections created successfully"); //检索集合 MongoCollection<Document> collection = database.getCollection("sampleCollection"); collection.drop(); System.out.println("Collection dropped successfully"); } }
在编译时,上述程序为您提供以下输出-
Connected to the database successfully Collection sampleCollection selected successfully Collection dropped successfully
列出所有集合
要列出数据库中的所有集合,您需要使用 com.mongodb.client.MongoDatabase 类的 listCollectionNames()方法。
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class ListOfCollection { public static void main( String args[] ) { //检索集合 MongoClient mongo = new MongoClient( "localhost" , 27017 ); //创建Mongo客户 MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //创建凭据 MongoDatabase database = mongo.getDatabase("myDb"); System.out.println("Collection created successfully"); for (String name : database.listCollectionNames()) { System.out.println(name); } } }
在编译时,上述程序为您提供以下输出-
Connected to the database successfully Collection created successfully myCollection myCollection1 myCollection5
其余的MongoDB方法 save(),limit(),skip(),sort()等的工作方式与后续教程中说明的相同。
参考链接
https://www.learnfk.com/mongodb/mongodb-java.html
标签:Java,database,MongoDB,successfully,无涯,client,mongodb,import,com From: https://blog.51cto.com/u_14033984/8181479