Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ MongoDB in bulk timestamp change the date format     - openSUSE 13.1 / 13.2 installation SoundConverter 2.1.2 (Linux)

- Oracle TAF Analysis (Database)

- CentOS permanently banned from running in the background PackageKit (Linux)

- Basic Tutorial: Linux novice should know 26 commands (Linux)

- The development environment to build Nodejs under Ubuntu 14.04 (Linux)

- Remote database using RMAN recovery test (RAC return to single-instance database) (Database)

- VirtualBox modify the size of the virtual machine disk VDI (Linux)

- ORA-4031 error Solution (Database)

- Linux RPM (Linux)

- Grub2: Save Your bootloader (Linux)

- Linux Workstation Security Checklist - from the Linux Foundation Internal (Linux)

- Use HugePages optimize memory performance (Database)

- Ubuntu users install the Download Manager software Xdman 5.0 (Linux)

- Boost-- time and date - (1) timer library introduction (Programming)

- Introduction to Linux system process monitoring tools (Linux)

- QEMU image file used: qcow2 and raw (Linux)

- Not safe procedure under Linux (Linux)

- Check the Linux server performance with ten or so commands in a minute (Server)

- How to use Linux iptables tool for network sharing (Linux)

- How to install Ubuntu applications Device 7 (Linux)

 
         
  MongoDB in bulk timestamp change the date format
     
  Add Date : 2018-11-21      
         
         
         
  1, mongodb official website provides traversal script:
Official documents Address: 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

Need to find all timestamp data records, because the timestamp starts with more numbers in recent years are 14XXX, so the regular expression to search for the beginning of 14, to achieve the following manner:

mongos> use pos
switched to db pos
mongos>
. Db.paymentinfo.find ({ "paymentTime": {$ regex: '14', $ options: 'i'}}) count ();
mongos> db.paymentinfo.find ({ "paymentTime": {$ regex: '144', $ options: 'i'}}). count ();
1995
mongos>

Queries to have 1995 records, find many, many records need to be addressed.

Establish traversal functions:

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: proof can not be used, the general effect

3, delete the first part of the expansion data txnType 1

Delete check out the collection of data

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

So check out the deleted records:

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

Remove paymentTime = 0 the data record

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

4, the remaining issues to be resolved
for (var i = 0, len = 3; i
Bulk edit data validation error date expectations to solve the problem

 db.paymentinfo.update ({ "_ id": ObjectId ( "55d56fdbe4b0c1f89b5356ae")}, {$ set: { "paymentTime": "14400511608049527"}}, true);
 var ds = db.paymentinfo.find ({ "paymentTime": {$ regex: '144', $ options: 'i'}});
 for (var i = 0, len = 1; i     var child = dschilds [i];
    var id = child._id;
    printjson (id);
    var paymentTime = child.paymentTime;
    var datestr = paymentTime
    # Question here, this date is the timestamp, for example 1440560826340 mode, may I ask, how in mongodb shell inside a timestamp into string '2015-12-15 12:34:16' This date string it?
    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 (a)});

See here does not solve my problem, so stop thinking, toLocaleString () to get the GST date format string, not what I need yyyy-mm-dd hh: mm: ss Date Format Number Structure

5, to find a breakthrough, using javascript
mongodb official website already given suggestive description, mongodb shell which can call javascript script, so to say, to write directly to the window inside js script to achieve ok, then be ready to take a piece of data to verify that the correct result is successful, the validation script as follows:
- A separate set of data, the time stamp into date string:

db.paymentinfo.find ({ "_ id": ObjectId ( "55d56cbbe4b0c1f89b5356a4")}). forEach (function (a) {
        # This function is in the month, day, hour, minute and second single digit preceded by 0 operation
        function tran_val (val) {
            if (parseInt (val) <10) {
                val = "0" + val;
            }
            return val;
        }
        # Here is paymentTime timestamp
        var datenew = new Date (parseInt (paymentTime));

        # Get date
        var year = datenew.getFullYear ();
        var month = tran_val (datenew.getMonth () + 1);
        var date = tran_val (datenew.getDate ());

        # Get minutes and seconds
        var hour = tran_val (datenew.getHours ());
        var minute = tran_val (datenew.getMinutes ());
        var second = tran_val (datenew.getSeconds ());

        # Assembled into a standard date format yyyy-mm-dd hh: mm: ss
        var datastr = year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second;
        a [ "paymentTime"] = datastr
        print (paymentTime);

        printjson (a)}
    );

The above example shows that the direct use of js script can change the date format to time stamp, then the following cycle begins for bulk edit:

  db.paymentinfo.update ({ "_ id": ObjectId ( "55d56fdbe4b0c1f89b5356ae")}, {$ set: { "paymentTime": "14400511608049527"}}, true);
    # Use iterate approach to the operation at the beginning of the time stamp 144
    var ds = db.paymentinfo.find ({ "paymentTime": {$ regex: '144', $ options: 'i'}});
    var dschilds = ds.toArray ();
    for (var i = 0; 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 to modify operations
        db.paymentinfo.update ({ "_ id": id}, {$ set: { "paymentTime": datestr}}, true);
        db.paymentinfo.find ({ "_ id": id});
    }

      # Use iterate approach to the operation at the beginning of the time stamp 145
    var ds = db.paymentinfo.find ({ "paymentTime": {$ regex: '145', $ options: 'i'}});
    var dschilds = ds.toArray ();
    for (var i = 0; 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, encountered new problems, unify date formats, will slash into bars
- Bulk modification date slashes into bars

    var ds = db.paymentinfo.find ({ "paymentTime": {$ regex: '/', $ options: 'i'}});
    var dschilds = ds.toArray ();
    for (var i = 0; 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});
    }
     
         
         
         
  More:      
 
- Linux modify the system time (Linux)
- Oracle Migration partition table (Database)
- Kubuntu 14.04 desktop to the user how to upgrade KDE 4.13.2 (Linux)
- Definition Format Oracle basis of various statements (Database)
- Use ISO document production OpenStack used CoreOS mirror (Linux)
- Use LKM change the default linux security level (Linux)
- VMware virtual machine to use bridged mode fast Internet access (Linux)
- MongoDB fragmentation (Cluster) (Database)
- Hadoop scheduling availability of workflow platform - Oozie (Server)
- 10 Codes of good practice PHP (Programming)
- Oracle 10g in the unique and index problems (Database)
- Use DB2 federated access Oracle (Database)
- 10 practical Java programming technology (Programming)
- C language - Traverse pci device (Programming)
- xCAT error Unable to dispatch hierarchical sub-command to NORESOLUTION: 3001 (Linux)
- Offline (local) Yum source structures (Linux)
- Run two MySQL service on one server (Database)
- Python3 multi-thread download codes (Programming)
- Ubuntu install Oracle 10g process and problem solution (Linux)
- Writing Better Bash build script 8 (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.