Another approach would be to use the $where operator in your find() method but note that the query will be fairly slow since using $where alone requires a table scan and the database executes the JavaScript expression or function for each document in the collection, so combine with indexed queries if you can as query performance also improves when you express it using the standard MongoDB operators (e.g., $gt, $in):db.test.find({ "$where": function() { return this.created.getDate() !== this.last_active.getDate() }});或更紧凑:db.test.find({ "$where": "this.created.getDate() !== this.last_active.getDate()" });使用输入:With the input:/* 0 */{ "_id" : 1, "created" : ISODate("2014-12-19T06:01:17.171Z"), "last_active" : ISODate("2014-12-21T15:38:13.842Z")}/* 1 */{ "_id" : 2, "created" : ISODate("2015-07-06T12:17:32.084Z"), "last_active" : ISODate("2015-07-06T18:07:08.145Z")}/* 2 */{ "_id" : 3, "created" : ISODate("2015-07-06T06:01:17.171Z"), "last_active" : ISODate("2015-07-07T10:04:30.921Z")}/* 3 */{ "_id" : 4, "created" : ISODate("2015-07-06T06:01:17.171Z"), "last_active" : ISODate("2015-07-06T09:47:44.186Z")}/* 4 */{ "_id" : 5, "created" : ISODate("2013-12-19T06:01:17.171Z"), "last_active" : ISODate("2014-01-20T13:21:37.427Z")}聚合返回:/* 0 */{ "result" : [ { "_id" : 1, "created" : ISODate("2014-12-19T06:01:17.171Z"), "last_active" : ISODate("2014-12-21T15:38:13.842Z"), "sameDay" : false }, { "_id" : 3, "created" : ISODate("2015-07-06T06:01:17.171Z"), "last_active" : ISODate("2015-07-07T10:04:30.921Z"), "sameDay" : false }, { "_id" : 5, "created" : ISODate("2013-12-19T06:01:17.171Z"), "last_active" : ISODate("2014-01-20T13:21:37.427Z"), "sameDay" : false } ], "ok" : 1} 这篇关于在mongo find方法中比较2个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-31 04:30