mongodb 查询某一天所有信息的3种方法,根据日期查询

// mongodb的查询真让人难以琢磨,就查询单天信息,都需要花费一番功夫才行。

// 第一种方式:
coll.aggregate([
          {$project:{sendDate: {$substr: ['$sendTime', 0, 10]}, sendTime: 1, content:1}},
          {$match:{sendDate: '2015-07-05'}},
        ])


// 第二种方式(第二种的变异):
coll.aggregate([
          {$match: {'sendTime': {'$gte': new Date('2015-07-05'), '$lt': new Date('2015-07-06')}}}

// 第三中方式(第二种的变异):
coll.aggregate([
          {$match: {'sendTime': {'$gte': new Date('2015-07-05 00:00:00'), '$lte': new Date('2015-07-05 23:59:59')}}}

 

// 查询结果如下(展示一种方式:其他展示略有不同):
[ { _id: 5599b09bc16aac90e9fb7995, sendDate: '2015-07-05' },
  { _id: 5599b161c16aac90e9fb7996, sendDate: '2015-07-05' },
  { _id: 5599b161c16aac90e9fb7997, sendDate: '2015-07-05' } ]

 

Name Description
$cmp Returns: 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second.
$eq Returns true if the values are equivalent.
$gt Returns true if the first value is greater than the second.
$gte Returns true if the first value is greater than or equal to the second.
$lt Returns true if the first value is less than the second.
$lte Returns true if the first value is less than or equal to the second.
$ne Returns true if the values are not equivalent.

你可能感兴趣的:(每天进步一点点,学习永无止境,mongodb,纵观千象)