In MongoDB, time stamps are converted to common date formats in batches.

Source: Internet
Author: User
Tags install mongodb

In MongoDB, time stamps are converted to common date formats in batches.

1. mongodb traversal script provided on the official website:
Official documentation: https://docs.mongodb.org/manual/tutorial/remove-documents/
> Var arr = ["AB", "cd", "ef"]
> Var show = function (value, index, ar) {print (value )}
> Arr. forEach (show)
AB
Cd
Ef

2. mongodb fuzzy query
Mongodb fuzzy query reference: You need to find all the data records of the timestamp. Because most timestamps start with numbers and are 14XXX in recent years, regular expressions start with 14 for search, the implementation method is as follows:

Mongos> use pos
Switched to db pos
Mongos>
Db. paymentinfo. find ({"paymentTime": {$ regex: '14', $ options: 'I'}). count ();
Mongos> db. paymentinfo. find ({"paymentTime": {$ regex: '000000', $ options: 'I'}). count ();
1995
Mongos>

1995 records are found, and a large number of records need to be processed.

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 is not usable, and the effect is average

3. First, delete part of the expansion data with txnType 1.

Delete the queried Set Data

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

Delete the queried records:

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

Delete data records with paymentTime = 0

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

4. Legacy problems need to be solved
For (var I = 0, len = 3; I <len; I ++) {var child = dschilds [I]; var id = child. _ id; printjson (id); var paymentTime = child. paymentTime; printjson (paymentTime )}

Expected solutions for batch modification of validation error date data

Db. paymentinfo. update ({"_ id": ObjectId ("55d56fdbe4b0c1f89b5356ae") },{$ set: {"paymentTime": "14400511608049527" }}, true );
Var ds = db. paymentinfo. find ({"paymentTime": {$ regex: '000000', $ options: 'I '}});
For (var I = 0, len = 1; I <len; I ++ ){
Var child = dschilds [I];
Var id = child. _ id;
Printjson (id );
Var paymentTime = child. paymentTime;
Var datestr = paymentTime
# The problem is that the date is a timestamp, for example, the 1440560826340 pattern, in mongodb shell, how does one change the timestamp to a date string like '2017-12-15 12:34:16?
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 {1, 2} $/, ''); printjson ()});

The problem cannot be solved here, so the idea is stopped. toLocaleString () obtains the Date Format String of GST, not the yyyy-mm-dd hh: mm: number of date formats of the ss Structure

5. Find a breakthrough and use javascript
The official mongodb website has reported an error message indicating that javascript scripts can be called in the mongodb shell. In this case, it is okay to directly write the js script in the window, then you have to take a piece of data to verify whether the result is correct. The verification script is as follows:
-In a separate set of data, the timestamp is converted into a date string:

Db. paymentinfo. find ({"_ id": ObjectId ("55d56cbbe4b0c1f89b5356a4")}). forEach (function (){
# This function is used to add 0 before a single digit in the month, day, and hour
Function tran_val (val ){
If (parseInt (val) <10 ){
Val = "0" + val;
}
Return val;
}
# Here, paymentTime is the timestamp.
Var datenew = new Date (parseInt (paymentTime ));

# Retrieve year, month, and day
Var year = datenew. getFullYear ();
Var month = tran_val (datenew. getMonth () + 1 );
Var date = tran_val (datenew. getDate ());

# Obtain the hour, minute, and second
Var hour = tran_val (datenew. getHours ());
Var minute = tran_val (datenew. getMinutes ());
Var second = tran_val (datenew. getSeconds ());

# Assemble it into the standard Date Format yyyy-mm-dd hh: mm: ss
Var datastr = year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second;
A ["paymentTime"] = datastr
Print (paymentTime );

Printjson ()}
);

The preceding example shows that you can use a js script to convert the time stamp to the date format. Then, you can start to modify the for Loop in batches:

Db. paymentinfo. update ({"_ id": ObjectId ("55d56fdbe4b0c1f89b5356ae") },{$ set: {"paymentTime": "14400511608049527" }}, true );
# Use the array Traversal method to operate the timestamp starting with 144
Var ds = db. paymentinfo. find ({"paymentTime": {$ regex: '000000', $ options: 'I '}});
Var dschilds = ds. toArray ();
For (var I = 0; I <dschilds. length; I ++ ){
Var child = dschilds [I];
Var id = child. _ id;
Var paymentTime = child. paymentTime;
Print (paymentTime );
Function tran_val (val ){
If (parseInt (val) <10 ){
Val = "0" + val;
}
Return val;
}
Var datenew = new Date (parseInt (paymentTime ));
Var year = datenew. getFullYear ();
Var month = tran_val (datenew. getMonth () + 1 );
Var date = tran_val (datenew. getDate ());
Var hour = tran_val (datenew. getHours ());
Var minute = tran_val (datenew. getMinutes ());
Var second = tran_val (datenew. getSeconds ());
Var datestr = year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second;
# Here we start the modification operation.
Db. paymentinfo. update ({"_ id": id}, {$ set: {"paymentTime": datestr }}, true );
Db. paymentinfo. find ({"_ id": id });
}

# Use the array Traversal method to operate the timestamp starting with 145
Var ds = db. paymentinfo. find ({"paymentTime": {$ regex: '000000', $ options: 'I '}});
Var dschilds = ds. toArray ();
For (var I = 0; I <dschilds. length; I ++ ){
Var child = dschilds [I];
Var id = child. _ id;
Var paymentTime = child. paymentTime;
Print (paymentTime );
Function tran_val (val ){
If (parseInt (val) <10 ){
Val = "0" + val;
}
Return val;
}
Var datenew = new Date (parseInt (paymentTime ));
Var year = datenew. getFullYear ();
Var month = tran_val (datenew. getMonth () + 1 );
Var date = tran_val (datenew. getDate ());
Var hour = tran_val (datenew. getHours ());
Var minute = tran_val (datenew. getMinutes ());
Var second = tran_val (datenew. getSeconds ());
Var datestr = year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second;
Db. paymentinfo. update ({"_ id": id}, {$ set: {"paymentTime": datestr }}, true );
Db. paymentinfo. find ({"_ id": id });
}

6. When a new problem occurs, unify the date format and change the slash into a horizontal bar.
-Batch modify the date slash to a horizontal bar

Var ds = db. paymentinfo. find ({"paymentTime": {$ regex: '/', $ options: 'I '}});
Var dschilds = ds. toArray ();
For (var I = 0; I <dschilds. length; I ++ ){
Var child = dschilds [I];
Var id = child. _ id;
Var paymentTime = child. paymentTime;
Var paymentTime2 = paymentTime. replace (// g ,"-");
Db. paymentinfo. update ({"_ id": id}, {$ set: {"paymentTime": paymentTime2 }}, true );
Print (paymentTime); print (paymentTime2 );
Db. paymentinfo. find ({"_ id": id });
}

OK. Thanks for the help of Aeolus @ pu, ^ _ ^.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.