import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class ConnectToDB { public static void main( String args[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB 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 = {}
}
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class CreatingCollection { public static void main( String args[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 创建集合 database.createCollection("tutorial"); System.out.println("集合创建成功"); } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
集合创建成功
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[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 创建集合 database.createCollection("tutorial"); System.out.println("集合创建成功"); // 检索集合 MongoCollection<Document> collection = database.getCollection("tutorial"); System.out.println("集合选择成功 tutorial"); } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
集合创建成功
集合选择成功 tutorial
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; public class InsertingDocument { public static void main( String args[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 创建集合 database.createCollection("tutorial"); System.out.println("集合创建成功"); // 检索集合 MongoCollection<Document> collection = database.getCollection("tutorial"); System.out.println("集合选择成功"); Document document = new Document("title", "MongoDB") .append("description", "database") .append("likes", 100) .append("url", "http://www.lmcjl.com/mongodb/") .append("by", "编程帮"); // 将文档插入到集合中 collection.insertOne(document); System.out.println("文档插入成功"); }编译并运行上面的程序,运行结果如下所示:
集合创建成功
集合选择成功
文档插入成功
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class RetrievingAllDocuments { public static void main( String args[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 连接到数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 检索集合 MongoCollection<Document> collection = database.getCollection("tutorial"); System.out.println("成功选择了集合 tutorial"); Document document1 = new Document("title", "MongoDB") .append("description", "database") .append("likes", 100) .append("url", "http://www.lmcjl.com/mongodb/") .append("by", "编程帮"); Document document2 = new Document("title", "html") .append("description", "database") .append("likes", 200) .append("url", "http://www.lmcjl.com/html/") .append("by", "编程帮"); List<Document> list = new ArrayList<Document>(); list.add(document1); list.add(document2); collection.insertMany(list); // 获取 iterable 对象 FindIterable<Document> iterDoc = collection.find(); int i = 1; // 获取迭代器 Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
成功选择了集合 tutorial
Document{{_id=5dce4e9ff68a9c2449e197b2, title=MongoDB, description=database, likes=100, url=http://www.lmcjl.com/mongodb/, by=编程帮}}
Document{{_id=5dce4e9ff68a9c2449e197b3, title=html, description=database, likes=200, url=http://www.lmcjl.com/html/, by=编程帮}}
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[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 连接到数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 检索集合 MongoCollection<Document> collection = database.getCollection("tutorial"); System.out.println("成功选择了集合 tutorial"); collection.updateOne(Filters.eq("title", 1), Updates.set("likes", 150)); System.out.println("文档更新成功..."); // 更新后检索文档 // 获取 iterable 对象 FindIterable<Document> iterDoc = collection.find(); int i = 1; // 获取迭代器 Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
成功选择了集合 tutorial
文档更新成功...
Document{{_id=5dce4e9ff68a9c2449e197b2, title=MongoDB, description=database, likes=100, url=http://www.lmcjl.com/mongodb/, by=编程帮}}
Document{{_id=5dce4e9ff68a9c2449e197b3, title=html, description=database, likes=200, url=http://www.lmcjl.com/html/, by=编程帮}}
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[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 检索集合 MongoCollection<Document> collection = database.getCollection("tutorial"); System.out.println("成功选择了集合 tutorial"); // 删除文档 collection.deleteOne(Filters.eq("title", "MongoDB")); System.out.println("文档删除成功..."); // 更新后检索文档 // 获取 iterable 对象 FindIterable<Document> iterDoc = collection.find(); int i = 1; // 获取迭代器 Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
成功选择了集合 tutorial
文档删除成功...
Document{{_id=5dce4e9ff68a9c2449e197b3, title=html, description=database, likes=200, url=http://www.lmcjl.com/html/, by=编程帮}}
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[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); // 创建集合 System.out.println("集合创建成功"); // 检索集合 MongoCollection<Document> collection = database.getCollection("tutorial"); // 删除集合 collection.drop(); System.out.println("集合删除成功"); } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
集合创建成功
集合删除成功
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class ListOfCollection { public static void main( String args[] ) { // 创建 MongoDB 连接 MongoClient mongo = new MongoClient( "localhost" , 27017 ); // 连接到 MongoDB MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("数据库连接成功"); // 访问数据库 MongoDatabase database = mongo.getDatabase("myDb"); System.out.println("集合创建成功"); for (String name : database.listCollectionNames()) { System.out.println(name); } } }编译并运行上面的程序,运行结果如下所示:
数据库连接成功
集合创建成功
myCollection
myCollection1
myCollection5
本文链接:http://task.lmcjl.com/news/16615.html