加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 与mongodb聚合中的子字符串匹配

发布时间:2020-12-14 06:02:02 所属栏目:百科 来源:网络整理
导读:在我的 mongodb系列中,我有一个time_stamp =“2013-06-30 23:58:37 928”. 我需要使用只有日期的“$match”,例如time_stamp =“2013-06-30”.那么我怎样才能得到那样的子串呢? 以前我尝试过使用$substr,但它显示错误“errmsg”:“exception:invalid opera
在我的 mongodb系列中,我有一个time_stamp =“2013-06-30 23:58:37 928”.
我需要使用只有日期的“$match”,例如time_stamp =“2013-06-30”.那么我怎样才能得到那样的子串呢?

以前我尝试过使用$substr,但它显示错误“errmsg”:“exception:invalid operator:$substr”

解决方法

我认为你试图使用聚合框架进行查询,因为你试过 $match& $substr操作符.我创建了一个简单的示例来说明如何使用 $substr来获得聚合框架所需的结果.

我已将以下数据插入MongoDB.

{ "_id" : ObjectId("528b343881d4fe2cfe0b1b25"),"time_stamp" : "2013-06-30 23:58:37 928" }
{ "_id" : ObjectId("528b343b81d4fe2cfe0b1b26"),"time_stamp" : "2013-06-30 23:58:37 928" }
{ "_id" : ObjectId("528b344c81d4fe2cfe0b1b27"),"time_stamp" : "2013-06-30 12:58:37 928" }
{ "_id" : ObjectId("528b344f81d4fe2cfe0b1b28"),"time_stamp" : "2013-06-30 12:58:23 928" }
{ "_id" : ObjectId("528b345381d4fe2cfe0b1b29"),"time_stamp" : "2013-06-31 12:58:23 928" }
{ "_id" : ObjectId("528b345981d4fe2cfe0b1b2a"),"time_stamp" : "2013-07-31 12:58:23 933" }

我使用$substr运算符编写了以下代码来按日期分组.

db.myObject.aggregate(
{$project : {new_time_stamp : {$substr : ["$time_stamp",10]}}},{$group:{_id:"$new_time_stamp","count": {$sum:1}}}
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读