網站首頁 編程語言 正文
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 ?=
原文鏈接:https://blog.csdn.net/lMasterSparkl/article/details/109679841
相關推薦
- 2022-05-06 golang導入私有倉庫報錯:“server response: not found:xxx: in
- 2022-05-25 詳解C++類的成員函數做友元產生的循環依賴問題_C 語言
- 2022-06-25 iOS開發CGContextRef畫圖使用總結_IOS
- 2022-06-22 Git?Bash終端默認路徑的設置查看修改及拓展圖文詳解_其它綜合
- 2024-07-15 golang使用migrate遷移pg數據庫表報錯處理
- 2022-11-05 python中的bisect模塊與二分查找詳情_python
- 2022-04-25 關于mongoDB數據庫添加賬號的問題_MongoDB
- 2022-04-02 C語言冒泡排序算法代碼詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支