mongo API用法示例

package util;

import java.net.UnknownHostException;
import java.util.Date;

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

 

public class MongoUtil {
 //返回表
 public static  DBCollection conn_mongo(String table){
  DBCollection Obtaintable = null;
  try {
   //得到连接
   Mongo mg = new Mongo("192.168.1.111",27017);//默认连本机127.0.0.1  端口为27017
   //得到数据库
   DB db = mg.getDB("quanzhiCV");
   //出示令牌以通行
//   db.authenticate("dev", "test".toCharArray());
   //得到表
   Obtaintable = db.getCollection(table);
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (MongoException e) {
   e.printStackTrace();
  }
  System.out.println(Obtaintable);
  return Obtaintable;
 }
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package test;

import java.net.UnknownHostException;

import org.junit.Test;

import util.MongoUtil;

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

public class Update {

 public void updateP_PersonalInformation1502() throws UnknownHostException, MongoException{
  //得到连接
  Mongo mg = new Mongo("192.168.1.111",27017);//默认连本机127.0.0.1  端口为27017
  //得到数据库
  DB db = mg.getDB("quanzhiCV");
  DBCollection P_PersonalInformation1502 = db.getCollection("P_PersonalInformation1502");
  
  DBObject queryCondition = new BasicDBObject(); 
  queryCondition.put("BirthdayY", 0);
  queryCondition.put("BirthdayM", 0);
  queryCondition.put("BirthdayD", 0);
       
        DBObject updateCondition = new BasicDBObject();
     DBObject incValue=new BasicDBObject();
     incValue.put("BirthdayY", null);
     incValue.put("BirthdayM", null);
     incValue.put("BirthdayD", null);
     
     updateCondition.put("$set", incValue);
  P_PersonalInformation1502.update(queryCondition, updateCondition,false,true);
  System.out.println("success");
 }
 
 
 public void updateP_Resume1502() throws UnknownHostException, MongoException{
  //得到连接
  Mongo mg = new Mongo("192.168.1.111",27017);//默认连本机127.0.0.1  端口为27017
  //得到数据库
  DB db = mg.getDB("quanzhiCV");
  DBCollection P_Resume1502 = db.getCollection("P_Resume1502");
  
  DBObject queryCondition = new BasicDBObject(); 
  queryCondition.put("BirthDay", 0);
       
        DBObject updateCondition = new BasicDBObject();
     DBObject incValue=new BasicDBObject();
     incValue.put("BirthDay", null);
     updateCondition.put("$set", incValue);
  
     P_Resume1502.update(queryCondition, updateCondition,false,true);
  System.out.println("success"); 
 }
 @Test
 public void updateXuda() throws UnknownHostException, MongoException{
  //得到连接
  Mongo mg = new Mongo("192.168.1.111",27017);//默认连本机127.0.0.1  端口为27017
  //得到数据库
  DB db = mg.getDB("quanzhiCV");
  DBCollection xuda = db.getCollection("xuda");
  System.out.println(xuda);
  
  DBObject queryCondition = new BasicDBObject(); 
  queryCondition.put( "Sex", 1);

  DBObject updateCondition = new BasicDBObject();
     DBObject incValue=new BasicDBObject(); 
     incValue.put("BirthdayY", null);
     incValue.put("BirthdayM", null);
     incValue.put("BirthdayD", null);
     
     updateCondition.put("$set", incValue);
        xuda.update(queryCondition, updateCondition,false,true);
 
 /* BasicDBObject newDocument3 = new BasicDBObject().append("$set",new BasicDBObject().append("is_Show",10000));
  xuda.update(new BasicDBObject().append("_id", "徐达"), newDocument3, true, true);
  System.out.println("success"); */
 }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package test;

import org.junit.Test;
import util.MongoUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

public class Select {
 /*P_PersonalInformation = db.getCollection("P_PersonalInformation1502"); 
 P_Resume = db.getCollection("P_Resume1502");*/
 private static int i = 0;
 @Test
 public void selectByIdFromMongo(){
//  DBCollection P_PersonalInformation1502 = MongoUtil.conn_mongo("P_PersonalInformation1502");
//  DBCollection P_Resume1502 = MongoUtil.conn_mongo("P_Resume1502");
  DBCollection xuda = MongoUtil.conn_mongo("xuda");
  //DBCollection collection = MongoUtil.conn_mongo();
  //查询 id的值为30000的记录  相当于where _id=30000"cust_Id": "123456"
  //DBCursor cur = xuda.find(new BasicDBObject("BirthdayY","0"));
  DBCursor cur = xuda.find(new BasicDBObject("is_Show", 1));
  while(cur.hasNext()){
   System.out.println(i++);
   System.out.println(cur.next());
  }

 }
 /*  //条件查询2 
      public void argsFind(){ 
         BasicDBObject condition = new BasicDBObject(); 
         //condition.put("age", new BasicDBObject("$gt", 20).append("$lte", 30));  
         condition.put("age", new BasicDBObject("$gt", 50)); 
         coll.find(condition); 
         //比较符 
         //"$gt": 大于 
         //"$gte":大于等于 
         //"$lt": 小于 
         //"$lte":小于等于 
         //"$in": 包含 
        //以下条件查询20<age<=30 
     } 
*/
 /*// 创建要查询的document
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("id", 1001);
    // 使用collection的find方法查找document
    DBCursor cursor = collection.find(searchQuery);
    //循环输出结果
    while (cursor.hasNext()) {
    System.out.println(cursor.next());
    }
    System.out.println("Done"); */
 /*@Test
 public void selectFromMongo(){
  DBCollection collection = MongoUtil.conn_mongo();
  //查询所有的数据 find() 注意他返回的是DBCursor
  DBCursor cur = collection.find();
  while(cur.hasNext()){
   System.out.println(cur.next());
  }

 }
 @Test
 public void selectByIdFromMongo(){
  DBCollection collection = MongoUtil.conn_mongo();
  //查询 id的值为30000的记录  相当于where _id=30000"cust_Id": "123456"
  DBCursor cur = collection.find(new BasicDBObject("cust_Id","123456"));
  while(cur.hasNext()){
   System.out.println(cur.next());
  }

 }
 @Test
 public void selectOneFromMongo(){
  DBCollection collection = MongoUtil.conn_mongo();
  //查询 id的值为30000的记录  相当于where _id=30000"cust_Id": "123456"
  System.out.println(collection.findOne(new BasicDBObject("cust_Id","123456"),new BasicDBObject("_id",true)));
 }
 
 @Test
 public void selectDueToIdFromMongo(){
  DBCollection collection = MongoUtil.conn_mongo();
  //查询所有的数据 find() 注意他返回的是DBCursor
  //查询id大于等于30000的记录  相当于where _id>=30000
  DBCursor cur = collection.find(new BasicDBObject("_id",new BasicDBObject("$gte",300)));
  while(cur.hasNext()){
   System.out.println(cur.next());
  }

 }
 @Test
 public void selectByPageFromMongo(){
  DBCollection collection = MongoUtil.conn_mongo();
  System.out.println(collection);
  //查询所有的数据 find() 注意他返回的是DBCursor
  //查询id大于等于30000的记录  相当于where _id>=30000
  DBCursor cur = collection.find(new BasicDBObject("_id",new BasicDBObject("$gte",1))).skip(10).limit(10);
  while(cur.hasNext()){
   System.out.println(cur.next());
  }
*/
 }
 
 
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package test;

import java.net.UnknownHostException;

import com.mongodb.MongoException;

public class Modify {

 /**
  * @param args
  * @throws MongoException
  * @throws UnknownHostException
  */
 public static void main(String[] args) throws UnknownHostException, MongoException {
  // TODO Auto-generated method stub
  Update update = new Update();
  update.updateP_PersonalInformation1502();
  update.updateP_Resume1502();
 }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package test;

import java.util.Date;

import org.junit.Test;

import util.MongoUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class Insert {
 @Test
 public void insert2mongo(){
  
  DBCollection collection = MongoUtil.conn_mongo("xuda");
  //第一种插入方式
  DBObject data1 = new BasicDBObject();
  data1.put("_id",  "徐达");
  data1.put("cust_Id", "123456");
  data1.put("cust_Name", "");
  data1.put("cust_BranchNo", "12345");
  data1.put("notice_Id", 2);
  data1.put("is_Show", 1);
  data1.put("start_time", new Date());
  data1.put("end_time",  new Date());
  data1.put("province_Id", "22");
  data1.put("notice_time_number", 20110616);
  collection.insert(data1);
  System.out.println("my name is nell");
 }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package test;

import org.junit.Test;

import util.MongoUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

public class Delete {

 @Test
 public void deleteDocument(){
  DBCollection users = MongoUtil.conn_mongo("xuda");
  //移除cust_Id为6533615的数据。注意 用remove方法不释放磁盘空间,
  //mongodb只在collection中做了标志,没有正在删除。
  users.remove(new BasicDBObject("_id","5056fb59109a2b22622933e4")).getN();

  //移除id>=1的数据
  //users.remove(new BasicDBObject("_id",new BasicDBObject("$gte",1))).getN();
  //移除整个collection,drop也不释放磁盘空间
  //users.drop();

 }
}

 

你可能感兴趣的:(mongo API用法示例)