skip to Main Content

Basically I want to update all documents inside one collection. The update is just adding 2 hours to date fields present in each document.
The documents all follow a basic structure like this :

    {
        code : 1,
        file : {
            dates : {
                start : 2018-05-27 22:00:00.000Z,
                end : 2018-05-27 22:00:00.000Z,
            },
            otherInfos : {
                ...
                ...
            }
        }
    }

Here is my query :

    var cursor = db.getCollection('files').find({});
        
    while(cursor.hasNext()){
        e = cursor.next();
        let delta = 120*60*1000; //2 hours
    
        if(e.file.dates) {
            let fileStartDate = e.file.dates.start ? new Date(e.file.dates.start.getTime() + delta) : null;
            let fileEndDate = e.file.dates.end ? new Date(e.file.dates.end.getTime() + delta) : null;
              
            if(fileStartDate) {
               e.file.dates.start = fileStartDate;
            }
            
            if(fileEndDate) {
               e.file.dates.end = fileEndDate;
            }
        }
        
        print(e);
        db.getMongo().getDB('myDB').files.updateOne(
            {"code":e.code},
            {
                $set: {"file.dates.start": fileStartDate, "file.dates.end": fileEndDate}
            })
    }

I am testing the query with around 20 documents and the first 10 are perfectly printed and updated with +2hours as expected but then for the second half the dates remain the exact same than before (both with the print and update).
All the documents have the same structure and same Date type so I don’t understand why the query doesn’t go all the way.

EDIT :
Here is a document that was succesfully updated :

    {
        "_id" : ObjectId("5b36c7fdd515e80009e7cc84"),
        "code" : "1",
        "file" : {
            "dates" : {
                "start" : ISODate("2018-06-11T22:00:00.000Z"),
                "end" : ISODate("2018-06-11T22:00:00.000Z")
            }
        }
    }

became as expected

    {
        "_id" : ObjectId("5b36c7fdd515e80009e7cc84"),
        "code" : "1",
        "file" : {
            "dates" : {
                "start" : ISODate("2018-06-12T00:00:00.000Z"),
                "end" : ISODate("2018-06-12T00:00:00.000Z")
            }
        }
    }

but for example this document :

    {
        "_id" : ObjectId("5b36c7ffd515e80009e7cf03"),
        "code" : "15",
        "file" : {
            "dates" : {
                "start" : ISODate("2018-09-02T22:00:00.000Z"),
                "end" : ISODate("2019-09-26T22:00:00.000Z")
            }
        }
    }

stayed the exact same

3

Answers


  1. db.getMongo().getDB('myDB').files.updateOne(
            {"code":e.code},
            {
                $set: {"file.dates.start": fileStartDate, "file.dates.end": fileEndDate}
            })
    

    updateOne only allows update on one document

    Login or Signup to reply.
  2. With MongoDBv4.2+, you can do an update with aggregation pipeline. Use $add to increment 2 hour * 60 minute * 60 seconds * 1000 milliseconds.

    db.collection.update({},
    [
      {
        "$set": {
          "file.dates.start": {
            $add: [
              "$file.dates.start",
              7200000
            ]
          },
          "file.dates.end": {
            $add: [
              "$file.dates.end",
              7200000
            ]
          }
        }
      }
    ],
    {
      multi: true
    })
    

    Here is the Mongo playground for your reference.

    Login or Signup to reply.
  3. You should use updateMany() to update more than 1 document
    https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search