日期:2014-05-16 浏览次数:20596 次
java 操作mongodb 类
package com.te.center;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gson.Gson;
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;
import com.mongodb.WriteResult;
import com.te.pojo.Driver;
import com.te.pojo.User;
/**
*
* @author yqf
*
*/
public class TaxiCenter {
private TaxiCenter(){}
private static Mongo mg = null;
private static DB db;
private static DBCollection drivers;
private static DBCollection users;
private static TaxiCenter tc = new TaxiCenter();
/*private Map<String,Timestamp> driverMap = new HashMap<String,Timestamp>();
private Map<String,Timestamp> userMap = new HashMap<String,Timestamp>(); */
private static boolean isInit = false;
public static TaxiCenter getTaxiCenter(){
init();
return tc;
}
private static void init() {
if(isInit== false){
try {
mg = new Mongo();
//mg = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
e.fillInStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
//获取temp DB;如果默认没有创建,mongodb会自动创建
db = mg.getDB("te");
//获取users DBCollection;如果默认没有创建,mongodb会自动创建
drivers = db.getCollection("drivers");
users = db.getCollection("users");
//创建二维索引
drivers.createIndex(new BasicDBObject("loc", "2d"));
drivers.ensureIndex("driverID");
drivers.ensureIndex("TIME");
users.createIndex(new BasicDBObject("loc", "2d"));
users.ensureIndex("userID");
users.ensureIndex("TIME");
isInit = true;
}
}
// //乘客队列
// private List<User> passs = new ArrayList<User>();
// //司机队列
// private List<Driver> drvers = new ArrayList<Driver>();
// //对话 key:接收方 value 对话
// private HashMap<Object, ArrayList<Talking>> talks = new HashMap<Object,ArrayList<Talking>>();
/*public synchronized List<Talking> addOrGetTalk(int command,Object receive, Object send, Talking talk){
if(command==0){ //获取消息
//返回处理
if(talks.containsKey(send)){
ArrayList<Talking> ret = talks.get(send);
talks.remove(send);
return ret;
} else {
return new ArrayList<Talking>();
}
} else if(command == 1){ //对话
if(talks.containsKey(receive)){
ArrayList<Talking> list = talks.get(receive);
list.add(talk);
} else {
ArrayList<Talking> list = new ArrayList<Talking>();
list.add(talk);
talks.put(receive, list);
}
//返回处理
if(talks.containsKey(send)){
ArrayList<Talking> ret = talks.get(send);
talks.remove(send);
return ret;
} else {
return new ArrayList<Talking>();
}
} else {
return new ArrayList<Talking>();
}
}*/
/**
* 增加乘客
* @param pass
*/
public void addPass(User pass){
Gson gson = new Gson();
double[] tt = {Double.parseDouble(pass.getLat()), Double.parseDouble(pass.getLng())};
DBObject passObj = new BasicDBObject();
passObj.put("loc", tt);
passObj.put("USER", gson.toJson(pass));
passObj.put("userID", pass.getUserId());
Timestamp time = new java.sql.Timestamp(System.currentTimeMillis());
passObj.put("TIME", time);
DBObject query = new BasicDBObject();
query.put("userID", pass.getUserId());
users.findAndModify(query, null, null, false, passObj, false, true);
//userMap.put(pass.getUserId(), time);
/*query
查询过滤条件
{}
sort
如果多个文档符合查询过滤条件,将以该参数指定的排列方式选择出排在首位的对象,该对象将被操作
{}
remove
若为true,被选中对象将在返回前被删除
N/A
update
一个 修改器对象
N/A
new
若为true,将返回修改后的对象而不是原始对象。在删除操作中,该参数被忽略。
false
fields
参见Retrieving a Subset of Fields (1.5.0+)
All fields
upsert
创建新对象若查询结果为空。 示例 (1.5.4+)*/
}
public int