当前位置:数据库 > mongodb >>

MongoDB学习(二)MongoDB Java增删查改

 
\
相关资料
1、MongoDB for Java的驱动包
  https://github.com/mongodb/mongo-java-driver/downloads
2、在线文档
 http://www.mongodb.org/display/DOCS/Java+Language+Center
 
操作
1、查询某张表(在MongoDB中称之为集合)的所有数据
Java代码DBTest.java
package com.archie.mongodb;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**

* 查询指定数据库指定DBCollection集合中的所有数据
* @author archie2010
*
* since 2012-9-29 下午10:40:21
*/
public class DBTest {
  public static void main(String[] args) throws UnknownHostException,
  MongoException {
  /**
Mongo实例代表了一个数据库连接池
* Mongo mg = new Mongo("localhost");
Mongo mg = new Mongo("localhost", 27017);
*/
  Mongo mg = new Mongo();
  // 获取名为“dbtest”的数据库对象
  DB db = mg.getDB("dbtest");
  // 查询该库中所有的集合 (相当于表)
  for (String name : db.getCollectionNames()) {
  System.out.println("Collection Name: " + name);
  }
  DBCollection users = db.getCollection("emp");
  // 查询集合中所有的数据
  DBCursor cur = users.find();
  System.out.println("Record Count:" + cur.count());
  while (cur.hasNext()) {
  DBObject object = cur.next();
  System.out.println(object);
  // 取出对象中列表为字段名为'uname'和'upwd'的数据
  System.out.println("uname:" + object.get("uname") + "\tupwd:"
  + object.get("upwd"));
  }
  }
}
运行结果:
\
2、对指定DBCollection集合的CRUD操作
Java代码
DBUtil.java
 
package com.archie.mongodb;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
/**
* 获得DBCollection集合的工具类
* @author archie2010
*
* since 2012-9-29 下午10:54:42
*/
public class DBUtil {
  public static Mongo mg=null; 
  public static DB db=null; 
  public static DBCollection collection;
  /**
* 获得DBCollection对象
* @param dbName
* @param colName
* @return
*/
  public static DBCollection getDBCollection(String dbName,String colName){
  if(mg==null){
  try {
  mg=new Mongo();
  } catch (UnknownHostException e) {
  e.printStackTrace();
  }
  }
  if(db==null){
  db=mg.getDB(dbName);
  }
  return db.getCollection(colName);
  }
}
CRUDTest.java
package com.archie.mongodb;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
/**

* 对指定DBCollection集合的CRUD操作
* @author archie2010
*
* since 2012-9-29 下午10:51:24
*/
public class CRUDTest {
  /**
* 增加
* @param obj
*/
  public static void add(DBObject obj){
  DBCollection coll=DBUtil.getDBCollection("dbtest", "emp");
  coll.insert(obj);
  }
  /**
* 删除
* @param obj
*/
  public static void delete(DBObject obj){
  DBCollection coll=DBUtil.getDBCollection("dbtest", "emp");
  coll.remove(obj);
  }
  /**
* 查询
*/
  public static void query(){
  DBCollection coll=DBUtil.getDBCollection("dbtest", "emp");
  // 查询集合中所有的数据
  DBCursor cur = coll.find();
  System.out.println("Record Count:" + cur.count());
  while (cur.hasNext()) {
  DBObject object = cur.next();
  System.out.println(object);
  // 取出对象中列表为'uname'和'upwd'的数据
  System.out.println("uname:" + object.get("uname") + "\tupwd:"
  + object.get("upwd")+"\t_id:"+object.get("_id"));
  }
  }
  /**
* 修改
*/
  public static void modify(DBObject orig,DBObject update){
  DBCollection coll=DBUtil.getDBCollection("dbtest", "emp");
  coll.update(orig, update, true, false);
  }
  public static void main(String[] args) {
  DBObject empObj=new BasicDBObject();
  empObj.put("uname", "teddy");
  empObj.put("upwd", "123456");
  //添加
  add(empObj);
  query();
 
  DBObject updateObj=new BasicDBObject();
  updateObj.put("uname", "teddy");
  updateObj.put("upwd", "3333");
  //更新
  modify(new BasicDBObject("uname","teddy"),updateObj);
  System.out.println("-----------------------修改后-------------------");
  query();
  //删除
  delete(new BasicDBObject("uname","teddy"));
  System.out.println("-----------------------删除后-------------------");
  query();
  }
}
运行效果:
\
 示例下载
 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,