MongoDB入门之增删改查(Java)

测试代码,请重构

	//枚举数据库
	private static void listDB()
	{
		MongoClient mongoClient = new MongoClient("localhost", 27017);
	    for (String dbName : mongoClient.listDatabaseNames())
	    {
	    	System.out.println("dbName: " + dbName);
	    }
	}
	
	//枚举collection
	private static void listCollection()
	{
		MongoClient mongoClient = new MongoClient("localhost", 27017);
		MongoDatabase db = mongoClient.getDatabase("test");
	    for (String collectionName : db.listCollectionNames())
	    {
	    	System.out.println("collectionName: " + collectionName);
	    }
	}

	//查询全部数据
	private static void testQueryAll()
	{
		MongoClient mongoClient = new MongoClient("localhost", 27017);
		MongoDatabase db = mongoClient.getDatabase("test");
		MongoCollection collection = db.getCollection("person");
		BasicDBObject query = new BasicDBObject();
		FindIterable iterable = collection.find(query);
		MongoCursor cursor = iterable.iterator();
		while (cursor.hasNext()) 
		{
		   org.bson.Document person = (org.bson.Document)cursor.next();
		   System.out.println(person.get("name"));
		   System.out.println(person.toString());
		}
		cursor.close();
	}

	//按条件查询数据
	private static void testQuery()
	{
		MongoClient mongoClient = new MongoClient("localhost", 27017);
		MongoDatabase db = mongoClient.getDatabase("test");
		MongoCollection collection = db.getCollection("person");
		
		BasicDBObject query = new BasicDBObject("name","Joe");
		FindIterable iterable = collection.find(query);
		MongoCursor cursor = iterable.iterator();
		while (cursor.hasNext()) 
		{
		   org.bson.Document person = (org.bson.Document)cursor.next();
		   System.out.println(person.get("name"));
		   System.out.println(person.toString());
		}
		cursor.close();
	}

	//插入
	private static void testInsert() 
	{
		MongoClient mongoClient = new MongoClient("localhost", 27017);
		MongoDatabase db = mongoClient.getDatabase("test");
		MongoCollection collection = db.getCollection("person");
		
		Document doc = new Document();
		doc.put("name", "tuzi");
		doc.put("age", 27);
		doc.put("sex", "Female");
		collection.insertOne(doc);
	}

	//删除
	private static void testDelete() {
		MongoClient mongoClient = new MongoClient("localhost", 27017);
		MongoDatabase db = mongoClient.getDatabase("test");
		MongoCollection collection = db.getCollection("person");
		BasicDBObject query = new BasicDBObject("name", "tuziki");
		collection.deleteMany(query);
	}

	//更新
	private static void testUpdate() {
		MongoClient mongoClient = new MongoClient("localhost", 27017);
		MongoDatabase db = mongoClient.getDatabase("test");
		MongoCollection collection = db.getCollection("person");
		
		BasicDBObject query = new BasicDBObject("name", "tuzi");
		
		BasicDBObject newDocument = new BasicDBObject();
		newDocument.put("name", "tuziki");
		
		BasicDBObject updateObj = new BasicDBObject();
		updateObj.put("$set", newDocument);
		
		collection.updateMany(query, updateObj);
	}

Leave a Reply

Your email address will not be published. Required fields are marked *

*