欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现MongoDB的简单操作

程序员文章站 2022-04-04 07:54:25
package test;import java.util.*;import org.bson.Document;import org.bson.conversions.Bson;import com.mongodb.Block;import com.mongodb.MongoClient;import com.mongodb.client.*;import com.mongo......
package test;

import java.util.*;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.*;
import com.mongodb.client.result.*;

import static com.mongodb.client.model.Filters.*;

public class MyJpanel {

	public static void main(String args[]) {
		try {
			// 连接Mongodb服务
			MongoClient mongoClient = new MongoClient("localhost", 27017);

			// 连接数据库
			MongoDatabase database = mongoClient.getDatabase("MYDB");

			// 连接集合
			MongoCollection<Document> collection = database.getCollection("cities");

			System.out.println("连接成功");
			
			mongoClient.close();
		} catch (Exception e) {
			System.err.println(e.getClass().getName() + ":" + e.getMessage());
		}
	}

	// 显示所有数据库
	public void ShowMongo(MongoClient mongoClient) {
		MongoIterable<String> dbs = mongoClient.listDatabaseNames();
		for (String db : dbs) {
			System.out.println(db);
		}
	}

	// 显示所有集合
	public void ShowMongoDatabase(MongoDatabase database) {
		MongoIterable<String> collections = database.listCollectionNames();
		for (String c : collections) {
			System.out.println(c);
		}
	}

	// 修改第一个符合条件的数据
	public void UpdateOne(MongoCollection<Document> collection) {
		collection.updateOne(eq("Population", "100000"), new Document("$set", new Document("Population", "99999")));
	}

	// 批量修改数据
	public void UpdateMany(MongoCollection<Document> collection) {
		 UpdateResult updateResult = collection.updateMany(lt("Population", "100000"),  
				 new Document("$inc", new Document("Population", "99999")));  
		 System.out.println(updateResult.getModifiedCount());  
	}

	// 删除第一个符合条件的数据
	public void DeleteOne(MongoCollection<Document> collection) {
		
		collection.deleteOne(eq("name", "哈尔滨"));
		
	}

	// 批量删除数据
	public void DeleteMany(MongoCollection<Document> collection) {
		
		DeleteResult deleteResult = collection.deleteMany(gte("Population", "100000"));
		System.out.println(deleteResult.getDeletedCount());
	}

	// 迭代器查找集合中的所有文档
	public void FindAll(MongoCollection<Document> collection) {

		MongoCursor<Document> cursor = collection.find().iterator();
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next().toJson());
			}
		} finally {
			cursor.close();
		}
	}

	// 创建数据集(增加)
	public void testinit(MongoCollection<Document> collection) {
		Document doc1 = new Document("name", "哈尔滨").append("Population", "100000");
		Document doc2 = new Document("name", "吉林").append("Population", "100001");
		Document doc3 = new Document("name", "沈阳").append("Population", "100002");
		Document doc4 = new Document("name", "北京").append("Population", "100003");
		Document doc5 = new Document("name", "济南").append("Population", "100004");
		Document doc6 = new Document("name", "石家庄").append("Population", "100005");
		Document doc7 = new Document("name", "广东").append("Population", "100006");
		Document doc8 = new Document("name", "合肥").append("Population", "100007");
		Document doc9 = new Document("name", "上海").append("Population", "100008");
		Document doc10 = new Document("name", "重庆").append("Population", "100009");
		List<Document> documents = new ArrayList<Document>();
		documents.add(doc1);
		documents.add(doc2);
		documents.add(doc3);
		documents.add(doc4);
		documents.add(doc5);
		documents.add(doc6);
		documents.add(doc7);
		documents.add(doc8);
		documents.add(doc9);
		documents.add(doc10);
		collection.insertMany(documents);
	}
	
	// 查找第一个符合条件的数据
	public void FindOne(MongoCollection<Document> collection) {
		Document doc = collection.find(eq("name", "哈尔滨")).first();
		System.out.println(doc);
	}
	
	// 批量查找数据
	public void FindMany(MongoCollection<Document> collection) {
		Block <Document> printBlock = new Block<Document>() {
			public void apply(final Document document) {
				System.out.println(document.toJson());
			}
		};
		collection.find(gt("Popution", 100002)).forEach(printBlock);
	}
}

 

本文地址:https://blog.csdn.net/mqc925900181/article/details/85922992

相关标签: 数据库