MongoDB操作之日期轉(zhuǎn)換方式(string、ISODate、時間戳)
MongoDB日期轉(zhuǎn)換(string、ISODate、時間戳)
String字符串轉(zhuǎn)ISODate
db.aj_hy_copy.find().forEach(function(doc) { doc.許可證有效期起始日期=new Date(doc.許可證有效期起始日期); db.aj_hy_copy.save(doc); })
ISODate轉(zhuǎn)時間戳
db.aj_hy.find().forEach(function(doc) { doc.許可證有效期截止日期=doc.許可證有效期截止日期.valueOf(); db.aj_hy.save(doc); })
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 數(shù)據(jù)庫直連測試 * @author fuhao * */ public class MongDBTest { public static void main(String[] args) throws Exception { List<ServerAddress> list = new ArrayList<ServerAddress>(); // 連接數(shù)據(jù)庫 ip 端口 list.add(new ServerAddress("10.39.XXX.XXX", 27010)); MongoClient mongoClient = new MongoClient(list); //數(shù)據(jù)庫名稱 DB psdoc = mongoClient.getDB("qa_db_center"); //表明 DBCollection collection=psdoc.getCollection("base_user_info"); BasicDBObject queryObject = null; // 時間查詢 數(shù)據(jù)庫看到的時間不是真實時間 加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("結(jié)束"); } }
上面的代碼中查詢時間 按mysql 的流程應(yīng)該查詢到 2018-03-29 15:59:06 到2018-03-29 16:30:46 這個區(qū)間的數(shù)據(jù),但是mongoDB不同,因為mongo中的date類型以UTC(Coordinated Universal Time)存儲,就等于GMT(格林尼治標準時)時間。
而系統(tǒng)時間使用的是GMT+0800時間,兩者正好相差8個小時。
也就是用java 代碼插入的時間類型的值都會被減8小時。這個坑挺大的不注意很容易出事。
展示一下對比數(shù)據(jù)便于理解
上面的圈是查詢的條件對應(yīng)數(shù)據(jù)庫中的數(shù)據(jù)是2018-03-29T08:30:36.310Z 如下圖,但是在java中你寫2018-03-29 08:30:36這個時間肯定查不到數(shù)據(jù)
對比得出數(shù)據(jù)庫中看到的時間和實際時間差8小時,但是查詢出來的結(jié)果時間還是會被轉(zhuǎn)換回來(不以時間為條件查詢的話基本沒什么問題)。
記錄一下mongoDB中查詢區(qū)間時間的執(zhí)行語句
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:字段名
比較符號對應(yīng)列表
$gt -------- greater than >
$gte --------- gt equal >=
$lt -------- less than <
$lte --------- lt equal <=
$ne ----------- not equal !=
$eq -------- equal =
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mongodb實現(xiàn)定時備份與恢復(fù)的方法教程
這篇文章主要給大家介紹了Mongodb實現(xiàn)定時備份與恢復(fù)的方法教程,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-06-06Spring Boot中使用MongoDB數(shù)據(jù)庫的方法
MongoDB是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bjson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點是他支持的查詢語言非常強大2018-02-02MongoDB入門教程之細說MongoDB數(shù)據(jù)庫的增刪查改操作
這篇文章主要介紹了MongoDB入門教程之細說MongoDB數(shù)據(jù)庫的增刪查改操作,本文環(huán)境是windows,所以以圖片形式講解,需要的朋友可以參考下2014-08-08MongoDB用Mongoose得到的對象不能增加屬性完美解決方法(兩種)
本文給大家分享兩種解決方案解決MongoDB用Mongoose得到的對象不能增加屬性問題,本文給大家介紹的非常詳細,需要的朋友參考下吧2017-11-11MongoDB排序時內(nèi)存大小限制與創(chuàng)建索引的注意事項詳解
在數(shù)據(jù)量超大的情形下,任何數(shù)據(jù)庫系統(tǒng)在創(chuàng)建索引時都是一個耗時的大工程,下面這篇文章主要給大家介紹了關(guān)于MongoDB排序時內(nèi)存大小限制與創(chuàng)建索引的注意事項的相關(guān)資料,需要的朋友可以參考下2022-05-05