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.MongoClient;
import com.mongodb.ServerAddress;
public class MongDBTest {
public static void main(String[] args) throws Exception {
List<ServerAddress> list=new ArrayList<ServerAddress>();
// 连接数据库 ip 端口
list.add(new ServerAddress(“10.39.XXX.XXX”, 27010));
MongoClient mongoClient=new MongoClient(list);
//数据库名称
DB psdoc=mongoClient.getDB(“qa_db_center”);
//表明
DBCollection collection=psdoc.getCollection(“base_user_info”);
BasicDBObject queryObject=null;
// 时间查询 数据库看到的时间不是真实时间 加8小时后才是正确的时间
DBObject dbObject=new BasicDBObject();
String startDate=”2018-03-29 15:59:06″;
String endDate=”2018-03-29 16:30:46″;
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
dbObject.put(“$gte”, sdf.parse(startDate));
dbObject.put(“$lte”, sdf.parse(endDate));
queryObject=new BasicDBObject();
queryObject.put(“create_time”,dbObject);
DBCursor find=collection.find(queryObject);
while (find.hasNext()) {
DBObject next=find.next();
Object real_name=next.get(“real_name”);
Object mobile=next.get(“mobile”);
Object create_time=next.get(“create_time”);
String str=sdf.format(create_time);
System.out.println(real_name +”=====”+mobile +”=====”+str);
}
System.out.println(“结束”);
}
}