繁体   English   中英

MongoDB选择不超过一周的数据

[英]MongoDB select data which is not older than a week

因此,我尝试选择不超过我一周的数据,我需要从mysql中获取以下内容:

WHERE date <= one week ago

这是我当前的查询:

$this->aggregate(
                array(
                    array('$sort' => array( '_id' => self::SORT_DESC)),
                    array('$match' => array( 'seen' => 0)),
                    array('$group' => array('_id'=>'$target_user',
                        'type' => array('$push'=> array('type' => '$type',
                            'title'=>'$title',
                            'link'=>'$link',
                            'dt'=>'$dt',
                            'own'=>'$own',
                            'usr'=>'$usr'))
                        )),
                    ));

$group的字段dt是一个字符串日期,其存储方式如下: 2013-10-14 14:53:11因此,基于dt键,我需要不超过一周的数据。

可以在Mongo中完成类似的事情吗?

使用MongoDate,您可以执行以下操作:

$this->aggregate(
    array(
       array('$match' => array( 'seen' => 0, 'dt' => array('$gte'=> new \MongoDate(strtotime('-1 week'))))),
       array('$sort' => array( '_id' => self::SORT_DESC)),
       array('$group' => array('_id' => '$target_user',
          'type' => array('$push'=> array('type' => '$type',
              'title'=>'$title',
              'link'=>'$link',
              'dt'=>'$dt',
              'own'=>'$own',
              'usr'=>'$usr'))
    )),
 ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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