简体   繁体   中英

Updating mongo documents based on date related condition

I have a MongoDB users collection. A user document is something like this:

{
  "_id" : "REtKBfimJhmGcRtgQ",
  "createdAt" : ISODate("2015-12-18T21:30:40.342Z"),      
  "username" : "ziedmahdi",      
  "firstName" : "Feki",      
  "schedules" : [{
      "date" : ISODate("2015-12-24T00:00:00Z"),
      "periodsOfTime" : [{
          "startTime" : "10:00",
          "endTime" : "11:00"
        }]
    }, {
      "date" : ISODate("2015-12-27T00:00:00Z"),
      "periodsOfTime" : [{
          "startTime" : "10:00",
          "endTime" : "11:00"
        }]
    }, {
      "date" : ISODate("2015-12-31T00:00:00Z"),
      "periodsOfTime" : [{
          "startTime" : "10:00",
          "endTime" : "11:00"
        }]
    }]
}

I want to remove from the user schedule, the elements that are between two given dates.

After using the following query,nothing happens.

db.users.update({   
   _id: "REtKBfimJhmGcRtgQ"
},{
    $pull: {
        schedules: {
            $elemMatch: { date: {
                $gte: ISODate("2015-12-24T00:00:00Z"),
                $lte: ISODate("2015-12-31T00:00:00Z")
            }}
        }
    }
});

Can any one help me??

Thank you very much

After some extra trials, I found the solution.

db.users.update({
    _id: "REtKBfimJhmGcRtgQ"
},{
    $pull: {
        schedules: {
            date: {
                $gte: ISODate("2015-12-24T00:00:00.000Z"),
                $lte: ISODate("2015-12-31T00:00:00.000Z")
            }
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM