龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > nosql开发 >

【转】java操作mongo简单实例

时间:2014-05-22 15:50来源:网络整理 作者:网络 点击:
分享到:
package maven.demo.test; import java.util.ArrayList; import java.util.List; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBOb ...
package maven.demo.test;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.QueryOperators;

public class MongoDB {
	private static void print(String str){
		System.out.println(str);
	}
	public static void main(String[] args) {
		try {
			//创建连接
			Mongo m=new Mongo("127.0.0.1", 27017);
			//得到数据库
			DB db=m.getDB("test");
			//得到所有数据库
//			List<String> colls=m.getDatabaseNames();
//			for(String str:colls){
//				System.out.println(str); 
//			}
			
//			//得到所有的集合(表)
//			for(String collection:db.getCollectionNames()){
//				System.out.println(collection); 
//			}
			
			//删除一个数据库
			//m.dropDatabase("sun");
			
			//得到sun表
			DBCollection coll=db.getCollection("things");
			
			//查看一个表的索引
//			for(DBObject index:coll.getIndexInfo()){
//				System.out.println(index); 
//			}
			
//			DBObject myDoc=coll.findOne();
//			System.out.println(myDoc); 
			
			//添加
//			BasicDBObject doc=new BasicDBObject();
//			doc.put("name", "sunshan");
//			doc.put("sex", "男");
//			doc.put("age", 22);
			//coll.insert(doc);

			//删除
			//coll.remove(doc);
			
//			BasicDBObject doc1=new BasicDBObject();
//			doc1.put("i", 0);
//			doc1.put("j", "foo");
//			BasicDBObject doc2=new BasicDBObject();
//			doc2.put("hello", "world");
//			doc1.put("doc2", doc2);
//			coll.insert(doc1);
			
			//修改
//			BasicDBObject doc3=new BasicDBObject();
//			doc3.put("x", 6);
//			BasicDBObject doc4=new BasicDBObject();
//			doc4.put("x", 1);
//			coll.update(doc3, doc4,true,false);
			//如果数据库不存在就添加 |多条修改 false只修改第一天,true如果有多条就不修改
			
			//条件查询
			//System.out.println(coll.find(doc4));
			
			//coll.findAndRemove(doc4);
			
//			//批量插入
//			List<DBObject> datas=new ArrayList<DBObject>();
//			for(int i=0;i<10;i++){
//				BasicDBObject bd=new BasicDBObject();
//				bd.put("name", "data");
//				bd.append("age", i);
//				datas.add(bd);
//			}
//			coll.insert(datas);
			
			//添加
//		    BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start();
//			documentBuilder.add("database", "mkyongDB");
//			documentBuilder.add("table", "hosting");
//			BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start();
//			documentBuilderDetail.add("records", "99");
//			documentBuilderDetail.add("index", "vps_index1");
//			documentBuilderDetail.add("active", "true");
//			documentBuilder.add("detail", documentBuilderDetail.get());
//			coll.insert(documentBuilder.get());
			
			//添加
//			Map<Object,Object> map=new HashMap<Object,Object>();
//			map.put("a", 1);
//			map.put("b", "b");
//			coll.insert(new BasicDBObject(map));
			
			//添加
//			String json ="{'1' : '1','2' : '2',"+"'11' : {'1' : 1, '2' : '2', '3' : '3'}}";
//			DBObject dbobject=(DBObject)JSON.parse(json);
//			coll.insert(dbobject);
			
			//更新
//			BasicDBObject bdo=new BasicDBObject();
//			bdo.put("x", 11);
//			coll.update(new BasicDBObject().append("x", 0), bdo);
			
			//更新
//			BasicDBObject bdo=new BasicDBObject().append("$inc", new BasicDBObject().append("x", 12));
//			coll.update(new BasicDBObject().append("x", 11), bdo);
			
			//更新 
			//如果不使用$set 直接是 age则所有的都会更新
			//根据age为9条件把name:data修改为 name:sun
//			BasicDBObject bdo=new BasicDBObject().append("$set", new BasicDBObject().append("name", "sunshan"));
//			coll.update(new BasicDBObject().append("age", 9), bdo);
			
			//更新 
			//根据name为data条件把age:批量修改为 age:age
//			BasicDBObject bdo=new BasicDBObject().append("$set", new BasicDBObject().append("age", "age"));
//			coll.update(new BasicDBObject().append("name", "data"), bdo,false, true);
			
			//查询age=1
//			print("find:"+coll.find(new BasicDBObject("age", 1)).toArray());
			//查询age<=1
//			print("find: "+coll.find(new BasicDBObject("age", new BasicDBObject("$lte", 1))).toArray());  
			//查询age>=1
//			print("fint: "+coll.find(new BasicDBObject("age", new BasicDBObject("$gte", 1))).toArray());
			//查询age!=1
//     		print("fint: "+coll.find(new BasicDBObject("age", new BasicDBObject("$ne", 1))).toArray());
			//查询age=1,2,3
//			print("fint: "+coll.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.IN ,new int[]{1,2,3}))).toArray());
			//查询age!=1,2,3
//			print("find: "+coll.find(new BasicDBObject("age" ,new BasicDBObject(QueryOperators.NIN ,new int[]{1,2,3}))).toArray());

//			print("find: "+coll.find(new BasicDBObject("age" ,new BasicDBObject(QueryOperators.EXISTS ,true))).toArray());
			//查询age属性
//			print("find: "+coll.find(null ,new BasicDBObject("age" ,true)).toArray());
			
//			List<DBObject> list=coll.find().toArray();
//			for(Object obj:list){
//				System.out.println(obj); 
//			}
			
			//true查询出来存在的  /false 查询出来不存在的
			//print(""+coll.find(new BasicDBObject("y",new BasicDBObject(QueryOperators.EXISTS,false))).toArray());
			
//			DBObject dbc=new BasicDBObject();
//			dbc.put("name", 1111);
//			List<DBObject> list=new ArrayList<DBObject>();
//			list.add(dbc);
//			System.out.println(coll.insert(list).getN()); 
				
//			//查询部分数据块
//			DBCursor cursor=coll.find().skip(1);
//			while(cursor.hasNext()){
//				System.out.println(cursor.next()); 
//			}

//			DBCursor cur=coll.find(); //DBCursor cur=coll.find().limit(2); 
//			while(cur.hasNext()){
//				System.out.println(cur.next());  
//			}
			//System.out.println(cur.getCursorId()+" "+cur.count()+" "+JSON.serialize(cur));   
			
			//条件查询
			BasicDBObject doc5=new BasicDBObject();
			doc5.put("$gt", 1);
			doc5.put("$lt", 3);
		    print("find 21<y<23:"+coll.find(new BasicDBObject("y", doc5)).toArray()); 
//			BasicDBObject doc5=new BasicDBObject();
//			doc5.put("$gt", 1);
//			doc5.put("$lt", 3);
//			BasicDBObject doc6=new BasicDBObject();
//			doc6.put("x", doc5);
//			System.out.println(coll.find(doc6));

} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

精彩图集

赞助商链接