mongodb将时间戳转换为年月日日期
使用datetostring 方法进行转换 并且通过format指定转换日期格式
integer userid=aaa; groupoperation groupoperation = aggregation.group("day").sum("money").as("todayincome").count().as("todaypaycount"); aggregation aggregation = aggregation.newaggregation( aggregation.match(criteria.where("userid").is(userid)), project("userid","money").andexpression("{$datetostring: {date: { $add: {'$createtime', [0]} }, format: '%y%m%d'}}", new date(28800000)).as("day"), groupoperation, sort(sort.direction.asc, "_id") );
注意:
1.必须使用 $datetostring: {date: { $add: 通过求和进行date数据转换 如果去掉后面的会报解析错误
org.springframework.data.mongodb.uncategorizedmongodbexception: command failed with error 16006 (location16006): 'can't convert from bson type long to date' on server localhost:50000. the full response is {"ok": 0.0, "errmsg": "can't convert from bson type long to date", "code": 16006, "codename": "location16006"}; nested exception is com.mongodb.mongocommandexception: command failed with error 16006 (location16006): 'can't convert from bson type long to date' on server localhost:50000. the full response is {"ok": 0.0, "errmsg": "can't convert from bson type long to date", "code": 16006, "codename": "location16006"}
2.必须增加 new date(28800000) 的时间 原因是增加了8个时区的偏移量
mongodb中的日期查询的坑
在熟悉monggodb的时候遇到了时间查询的问题代码如下:
import java.text.simpledateformat; 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; /** * mongo 数据库直连测试 * @author fuhao * */ 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("结束"); } }
请求页面 默认页 https://blog.csdn.net/qq_27292113/article/details/91876121 【标题】:mongodb中的日期查询的坑_天马行空-的博客-csdn博客_mongodb query 日期 【内容】:
在熟悉monggodb的时候遇到了时间查询的问题代码如下:
上面的代码中查询时间 按mysql 的流程应该查询到 2018-03-29 15:59:06 到2018-03-29 16:30:46 这个区间的数据,但是mongodb不同,因为mongo中的date类型以utc(coordinated universal time)存储,就等于gmt(格林尼治标准时)时间。而系统时间使用的是gmt+0800时间,两者正好相差8个小时。也就是用java 代码插入的时间类型的值都会被减8小时。这个坑挺大的不注意很容易出事。
展示一下对比数据便于理解:
上面的圈是查询的条件对应数据库中的数据是2018-03-29t08:30:36.310z 如下图,但是在java中你写2018-03-29 08:30:36这个时间肯定查不到数据
对比得出数据库中看到的时间和实际时间差8小时,但是查询出来的结果时间还是会被转换回来(不以时间为条件查询的话基本没什么问题)。
记录一下mongodb中查询区间时间的执行语句:
db.getcollection('base_user_info').find({"create_time":{"$gte":isodate("2018-03-29 07:59:06"),"$lte":isodate("2018-03-29 08:30:46")}});
base_user_info :表名 create_time:字段名
比较符号对应列表
- $gt -------- greater than >
- $gte --------- gt equal >=
- $lt -------- less than <
- $lte --------- lt equal <=
- $ne ----------- not equal !=
- $eq -------- equal =
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。