关闭

Java程序中操作mongodb

标签: mongodbjava
198人阅读 评论(0) 收藏 举报
分类:

        之前说的都是在cmd窗口下操作mongodb数据库,现在介绍在java程序中操作的方式,与jdbc类似,也是先建立连接,然后再操作。

       1.导入jar包,以eclipse为例,build path --》add external achive  引入jar包,这里用mongo-java-driver-1.0.jar

       2.建立连接,多种方式

       Mongo mg=new Mongo();    默认连接到本机,端口为默认端口27017 

       Mongo mg=new Mongo("localhost",27017);   连接到本机,端口可以指定

       Mongo mg=new Mongo("ip",27017);    指定ip,指定端口,注意ip需要用双引号括起来

      3.获取数据库

       DB db=mg.getDB("test");     获取指定数据库

      4.获取集合

      DBCollection person=db.getCollection("person");

     5.sql操作

     DBCursor cur=person.find();   查询所有集合所有数据,遍历游标即可获取所有数据

     person.find(new BasicDBObject("name","lisi"));  查找指定的文档,如name为lisi

     

     DBObject obj=new BasicDBObject();

     obj.put("name","wangwu");

     person.insert(obj);     插入文档


     DBObject array=new BasicDBObject[2];

     array[0]=new BasicDBObject(“name”,”lisi”).append(“age”,18);

     array[1]=new BasicDBObject(“name”,”wangwu”).append(“age”,22);

     person.insert(array);    批量插入记录


        person.remove(new BasicDBObject("name","lisi"));    删除name为lisi的文档

     person.ermove(new BasicDBObject());      删除所有的文档


     person.update(new BasiceDBObject("name","lisi"),new BasiceDBObject("name","lisi").append("age",19));

     找到name为lisi的,修改它的age为19


     以下为源码

    

package mongodb.test;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoDB_Java {
    
	public static void main(String[] args) throws UnknownHostException, MongoException {
		//connect();
		//queryAll();
		//insert();
		//insertBatch();
		//removeOne();
		removeAll();
		//update();
	}
	private static void connect() throws UnknownHostException, MongoException{
		Mongo mg=new Mongo();
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
		System.out.println(person.getCount());
	}
	
	private static void queryAll()throws UnknownHostException, MongoException{
		Mongo mg=new Mongo("localhost",27017);
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
		DBCursor cur=person.find();
		while(cur.hasNext()){
			System.out.println(cur.next());
		}
	}
	
	private static void insert() throws UnknownHostException, MongoException{
		Mongo mg=new Mongo("192.168.42.182",27017);
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
		DBObject obj=new BasicDBObject();
		obj.put("name","zhaoliu");
		obj.put("age",30);
		person.insert(obj);
	}
	
	private static void insertBatch() throws UnknownHostException, MongoException{
		Mongo mg=new Mongo();
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
//		DBObject[] array=new BasicDBObject[3];
//		array[0]=new BasicDBObject("name","tianqi").append("age",44);
//		array[1]=new BasicDBObject("name","wangba").append("age",56);
//		array[2]=new BasicDBObject("name","linjiu").append("age",22);
//		person.insert(array);
		
		List<DBObject> list=new ArrayList<DBObject>();
		DBObject user=new BasicDBObject("name","lishi").append("age",12);
		DBObject user2=new BasicDBObject("name","luoshiyi").append("age",18);
		DBObject user3=new BasicDBObject("name","chenshier").append("age",28);
		list.add(user);
		list.add(user2);
		list.add(user3);
		person.insert(list);
	}
	
	private static void removeOne() throws UnknownHostException, MongoException{
		Mongo mg=new Mongo();
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
		person.remove(new BasicDBObject("name","chenshier"));
	}
	
	private static void removeAll() throws UnknownHostException, MongoException{
		Mongo mg=new Mongo();
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
		person.remove(new BasicDBObject());
	}
	
	private static void update()throws UnknownHostException, MongoException{
		Mongo mg=new Mongo();
		DB db=mg.getDB("test");
		DBCollection person=db.getCollection("person");
		//person.update(new BasicDBObject("name","zhangsan"),
		//		new BasicDBObject("name","zhangsan").append("age",88));
		person.updateMulti(new BasicDBObject("name","lisi"), 
				new BasicDBObject("name","lisi").append("age",66));
	}
}


         

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4864次
    • 积分:183
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类