Compare two date fields in MongoDB

前端 未结 6 1326
别跟我提以往
别跟我提以往 2020-12-10 04:36

in my collection each document has 2 dates, modified and sync. I would like to find those which modified > sync, or sync does not exist.

I tried

{\'m         


        
6条回答
  •  执念已碎
    2020-12-10 05:32

    Simply

    db.collection.find({$where:"this.modified>this.sync"})
    

    Example

    Kobkrits-MacBook-Pro-2:~ kobkrit$ mongo
    MongoDB shell version: 3.2.3
    connecting to: test
    > db.time.insert({d1:new Date(), d2: new Date(new Date().getTime()+10000)})
    WriteResult({ "nInserted" : 1 })
    > db.time.find()
    { "_id" : ObjectId("577a619493653ac93093883f"), "d1" : ISODate("2016-07-04T13:16:04.167Z"), "d2" : ISODate("2016-07-04T13:16:14.167Z") }
    > db.time.find({$where:"this.d1 db.time.find({$where:"this.d1>this.d2"})
    > db.time.find({$where:"this.d1==this.d2"})
    > 
    

提交回复
热议问题