Java实现MongoDB的简单操作

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);
	}
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值