Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert timestamp to date format in MongoDB

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to convert the timestamp into a date format in MongoDB. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Example of MongoDB fuzzy query method:

Fuzzy query: tname contains a keyword test'

Cd / opt/soft/mongodb/bin./mongo-- host 192.168.0.1-- port 17017 testdb.test_info.find ({"tname": {$regex: 'test', $options:'i'}}) db.test_info.find ({"tname": {$regex:/ test. * / I}})

You need to find data records for all timestamps, because timestamps start with numbers, and in recent years, they are all 14XXX, so regular expressions are searched at the beginning of 14. The implementation is as follows:

Mongos > use posswitched to db posmongos > db.paymentinfo.find ({"paymentTime": {$regex: '14percent, $options:'i'}}) .count (); mongos > db.paymentinfo.find ({"paymentTime": {$regex:' 144percent, $options:'i'}}) .count (); 1995mongos >

Query found that there are 1995 records, quite a lot, need to deal with a large number of records.

Create a traversal function:

Db.cms_Content.find ({"userId": "444333f107624489bae28140d1970bbc"}) .forEach (function (x) {if (x.title&&x.fileName) {print (x.contentId); db.cms_Content.update ({"contentId": x.contentId}, {"$set": {"title": x.fileName}});})

PS: the proof doesn't work, but the effect is average.

3. First delete some of the data with TXNTYPE 1

Delete the queried collection data

Db.paymentinfo.remove ({"txnType": {$regex: '1percent, $options:'i'}}, 300)

Delete all queried records:

Db.paymentinfo.remove ({"txnType": {$regex: '1percent, $options:'i'}})

Delete data records for paymentTime=0

Db.paymentinfo.remove ({paymentTime: "0"})

4. The remaining problems need to be solved.

For (var I = 0, len = 3; I < len; iTunes +) {var child=dschilds [I]; var id=child._id; printjson ((id)); var paymentTime=child.paymentTime; printjson (paymentTime)} problems expected to be solved by batch modification of parity error date data

Db.paymentinfo.update ({"_ id": ObjectId ("55d56fdbe4b0c1f89b5356ae")}, {$set: {"paymentTime": "14400511608049527"}, true); var ds= db.paymentinfo.find ({"paymentTime": {$regex: '144mm, $options:'i'}}); for (var I = 0, len = 1; I < len; iTunes +) {var child=dschilds [I]; var id=child._id; printjson (id); var paymentTime=child.paymentTime The problem with var datestr=paymentTime # is that this date is a timestamp, such as the pattern of 1440560826340. How to change the timestamp into a date string like '2015-12-15 12-15-12-14-12-15-34-14' in mongodb shell? Db.paymentinfo.update ({"_ id": id}, {$set: {"paymentTime": datestr}}, true); db.paymentinfo.find ({"_ id": id});} db.paymentinfo.find ({"_ id": ObjectId ("55dd36dc45ce9e75b91eb340")}) .forEach (function (a) {a ["paymentTime"] = new Date (parseInt (paymentTime) * 1000) .toLocaleString () .replace (/:\ d {1pm 2} $/,'); printjson (a)})

Seeing here doesn't solve my problem, so the train of thought stops. ToLocaleString () gets the date format string of GST, not the number of date formats I need for yyyy-mm-dd hh:mm:ss structure.

5. Find a breakthrough and use JAVASCRIPT

The official mongodb website has already reported an error. JavaScript script can be called in mongodb shell. In this way, you can write a js script in the window to implement ok, and then you are going to take a piece of data to verify whether it is correct. The verification script is as follows:-in a single collection data, change the timestamp into a date string:

Db.paymentinfo.find ({"_ id": ObjectId ("55d56cbbe4b0c1f89b5356a4")}) .forEach (function (a) {# this function is function tran_val (val) {if (parseInt (val)), which is operated by padding zero before the single digit of month, day, hour, minute and second.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report