当前位置: 代码网 > it编程>数据库>Nosql > Mongodb 如何将时间戳转换为年月日日期

Mongodb 如何将时间戳转换为年月日日期

2024年05月19日 Nosql 我要评论
mongodb将时间戳转换为年月日日期使用datetostring 方法进行转换 并且通过format指定转换日期格式        integer userid=aaa;        groupo

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  =

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

  • MongoDB对Document(文档)的插入、删除及更新

    一.document数据插入1.插入文档db.[文档名].insert({bson数据})2.批量插入文档shell当中不支持批量插入,想完成批量插入操作,可以使用shell的fo…

    2024年05月19日 数据库
  • MongoDB Shell常用基本操作命令详解

    MongoDB Shell常用基本操作命令详解

    mongodb shellmongodb shell 是一个功能齐全的 js 和 node.js 的repl环境,用于与 mongodb 服务器进行交互,是一个... [阅读全文]
  • MongoDB中实现多表联查的实例教程

    MongoDB中实现多表联查的实例教程

    前些天遇到一个需求,不复杂,用 sql 表现的话,大约如此:没想到搜了半天,我厂的代码仓库里没有这种用法,各种教程也多半只针对合并查询(即只筛选db1,没有db... [阅读全文]
  • MongoDB集合中的文档管理

    ,集合里面存放的是文档,因此聪明的你应该能想到这篇是学习文档管理。要说标题应该是文档管理,不过对于文档的管理都是先获得集合对象,在集合对象上调用方法管理文档,所以标题还是对集合的管…

    2024年05月19日 数据库
  • MongoDB集合的增删改查管理

    MongoDB集合的增删改查管理

    ,今天学习下集合的管理。对于这些基本都是增删改查。一、显示数据库中的集合列表参考之前显示数据库的列表可以猜测下可能也是使用show,集合的话那可能是复数形式,因... [阅读全文]
  • MongoD管理数据库的方法介绍

    今天主要了解数据库管理,在操作之前先把mongodb服务打开。一、显示数据库清单如果想查看数据库列表可以使用show dbs.二、切换数据库通过使用mongodb中内置的句柄db来…

    2024年05月19日 数据库

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com