Mongodb 如何將時(shí)間戳轉(zhuǎn)換為年月日日期
Mongodb將時(shí)間戳轉(zhuǎn)換為年月日日期
使用dateToString 方法進(jìn)行轉(zhuǎn)換 并且通過(guò)format指定轉(zhuǎn)換日期格式
? ? ? ? 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: 通過(guò)求和進(jìn)行date數(shù)據(jù)轉(zhuǎn)換 如果去掉后面的會(huì)報(bào)解析錯(cuò)誤
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) 的時(shí)間 原因是增加了8個(gè)時(shí)區(qū)的偏移量
MongoDB中的日期查詢的坑
在熟悉monggoDB的時(shí)候遇到了時(shí)間查詢的問(wèn)題代碼如下:
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 數(shù)據(jù)庫(kù)直連測(cè)試 * @author fuhao * */ public class MongDBTest { public static void main(String[] args) throws Exception { List<ServerAddress> list = new ArrayList<ServerAddress>(); // 連接數(shù)據(jù)庫(kù) ip 端口 list.add(new ServerAddress("10.39.XXX.XXX", 27010)); MongoClient mongoClient = new MongoClient(list); //數(shù)據(jù)庫(kù)名稱 DB psdoc = mongoClient.getDB("qa_db_center"); //表明 DBCollection collection=psdoc.getCollection("base_user_info"); BasicDBObject queryObject = null; // 時(shí)間查詢 數(shù)據(jù)庫(kù)看到的時(shí)間不是真實(shí)時(shí)間 加8小時(shí)后才是正確的時(shí)間 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("結(jié)束"); } }
請(qǐng)求頁(yè)面 默認(rèn)頁(yè) https://blog.csdn.net/qq_27292113/article/details/91876121 【標(biāo)題】:MongoDB中的日期查詢的坑_天馬行空-的博客-CSDN博客_mongodb query 日期 【內(nèi)容】:
在熟悉monggoDB的時(shí)候遇到了時(shí)間查詢的問(wèn)題代碼如下:
上面的代碼中查詢時(shí)間 按mysql 的流程應(yīng)該查詢到 2018-03-29 15:59:06 到2018-03-29 16:30:46 這個(gè)區(qū)間的數(shù)據(jù),但是mongoDB不同,因?yàn)閙ongo中的date類型以UTC(Coordinated Universal Time)存儲(chǔ),就等于GMT(格林尼治標(biāo)準(zhǔn)時(shí))時(shí)間。而系統(tǒng)時(shí)間使用的是GMT+0800時(shí)間,兩者正好相差8個(gè)小時(shí)。也就是用java 代碼插入的時(shí)間類型的值都會(huì)被減8小時(shí)。這個(gè)坑挺大的不注意很容易出事。
展示一下對(duì)比數(shù)據(jù)便于理解:
上面的圈是查詢的條件對(duì)應(yīng)數(shù)據(jù)庫(kù)中的數(shù)據(jù)是2018-03-29T08:30:36.310Z 如下圖,但是在java中你寫2018-03-29 08:30:36這個(gè)時(shí)間肯定查不到數(shù)據(jù)
對(duì)比得出數(shù)據(jù)庫(kù)中看到的時(shí)間和實(shí)際時(shí)間差8小時(shí),但是查詢出來(lái)的結(jié)果時(shí)間還是會(huì)被轉(zhuǎn)換回來(lái)(不以時(shí)間為條件查詢的話基本沒(méi)什么問(wèn)題)。
記錄一下mongoDB中查詢區(qū)間時(shí)間的執(zhí)行語(yǔ)句:
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:字段名
比較符號(hào)對(duì)應(yīng)列表
- $gt -------- greater than >
- $gte --------- gt equal >=
- $lt -------- less than <
- $lte --------- lt equal <=
- $ne ----------- not equal !=
- $eq -------- equal =
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MongoDB連接數(shù)據(jù)庫(kù)并創(chuàng)建數(shù)據(jù)等使用方法
MongoDB?是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。接下來(lái)通過(guò)本文給大家介紹MongoDB連接數(shù)據(jù)庫(kù)并創(chuàng)建數(shù)據(jù)等使用方法,感興趣的朋友一起看看吧2021-11-11Mac下安裝配置mongodb并創(chuàng)建用戶的方法
最近在在學(xué)習(xí)nodejs,相比mysql,mongodb與nodejs搭配更合適,存儲(chǔ)數(shù)據(jù)格式也比較接近JS對(duì)象。下面這篇文章主要給大家介紹了關(guān)于在Mac下安裝配置mongodb并創(chuàng)建用戶的相關(guān)資料,需要的朋友可以參考下2018-05-05MongoDB中實(shí)現(xiàn)多表聯(lián)查的實(shí)例教程
數(shù)據(jù)庫(kù)應(yīng)用在我們的生活中是很常見(jiàn)的,在編輯一些應(yīng)用以及軟件的時(shí)候都需要用到數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MongoDB中實(shí)現(xiàn)多表聯(lián)查的相關(guān)資料,需要的朋友可以參考下2022-07-07Mongodb 3.2.9開(kāi)啟用戶權(quán)限認(rèn)證問(wèn)題的步驟詳解
這篇文章主要給大家介紹了關(guān)于Mongodb 3.2.9開(kāi)啟用戶權(quán)限認(rèn)證問(wèn)題的詳細(xì)步驟,通過(guò)開(kāi)啟權(quán)限認(rèn)證,會(huì)對(duì)大家的Mongodb更加保護(hù)的安全些,文中將步驟介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08MongoDB中aggregate()方法實(shí)例詳解
MongoDB中聚合(aggregate)主要用于處理數(shù)據(jù)(諸如統(tǒng)計(jì)平均值,求和等),并返回計(jì)算后的數(shù)據(jù)結(jié)果,下面這篇文章主要給大家介紹了關(guān)于MongoDB中aggregate()方法的相關(guān)資料,需要的朋友可以參考下2023-01-01mongodb+php實(shí)現(xiàn)簡(jiǎn)單的增刪改查
這篇文章主要介紹了mongodb+php實(shí)現(xiàn)簡(jiǎn)單的增刪改查的相關(guān)資料,需要的朋友可以參考下2016-07-07mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼
這篇文章主要介紹了 mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01