zoukankan      html  css  js  c++  java
  • Data type conversion in MongoDB

    [问题]

    I have a collection called Document in MongoDB. Documents in this collection have a field called CreationDate stored in ISO date type. My task is to count the number of documents created per day and sort by the number asynchronously. The output format is required to be [{_id:'yyyy-MM-dd', cnt:x}]. I tried to use aggregation framework as below.

    db.Document.aggregate(

        , {$project: {_id:1, Year:{$year:'$CreationDate'}, Month:{$month:'$CreationDate'}, Date:{$dayOfMonth:'$CreationDate'}}}

        , {$group: {_id:{$concat:['$Year', '-', '$Month', '-', '$Date']}, cnt:{$sum:1}}}

        , {$sort:{'cnt':-1}}

    );

    The code gives me error as below:

    $concat only supports strings, not NumberInt32

    I understand this is because $year, $month and $dayOfMonth all return number. It's possible to compose the _id field as an object and re-format it in the desired format in application level.

    But from technical perspective, I have two questions:

    1. How to convert a number to string in MongoDB shell? In this case, output of $year can then be converted to string and used in $concat.
    2. Is there a better way to format ISODate output to various date formats? In many cases, we only need certain part of an ISODate, for example: the date component or the time portion. Is there any MongoDb inbuilt operators to achieve this?

    Thanks in advance for any advice.

     [回答]

    You can do this with $concat but first you need to convert to a string via $substr, also handling the double digit case:

    db.Document.aggregate([ 
        { "$group": { 
            "_id":{ 
                "$concat": [
                     { "$substr": [ { "$year": "$CreationDate" }, 0, 4 ] },
                     "-",
                     { "$cond": [
                         { "$gt": [ { "$month": "$CreationDate" }, 9 ] },
                         { "$substr": [ { "$month": "$CreationDate" }, 0, 2 ] },
                         { "$concat": [
                             "0",
                             { "$substr": [ { "$month": "$CreationDate" }, 0, 1 ] },
                         ]},
                     ]},
                     "-",
                     { "$cond": [
                         { "$gt": [ { "$dayOfMonth": "$CreationDate" }, 9 ] },
                         { "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 2 ] },
                         { "$concat": [
                             "0",
                             { "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 1 ] },
                         ]}
                     ]}
                 ]
             },
             { "cnt": { "$sum": 1 } }
        }}
        { "$sort":{ "cnt" :-1 }}
    ]);

    Possibly better is to just use date math instead, this returns an epoch timestamp value, but it is easy to work into a date object in post processing:

    db.Document.aggregate([
        { "$group": {
            "_id": {
                "$subtract": [
                    { "$subtract": [ "$CreationDate", new Date("1970-01-01") ] },
                    { "$mod": [
                        { "$subtract": [ "$CreationDate", new Date("1970-01-01") ] },
                        1000 * 60 * 60 * 24
                    ]}
                ]
            },
            "cnt": { "$sum": 1 }
        }},
        { "$sort": { "cnt": -1 } }
    ])

    来自: https://stackoverflow.com/questions/25176855/data-type-conversion-in-mongodb

  • 相关阅读:
    2018最新php笔试题及答案(持续更新)
    快速上手模板制作
    春节期间小游戏同时在线人数最高达2800万人/小时
    公众平台新增修改文章错别字功能 每篇文章允许被修改一次仅限正文内五个字
    微信6.6.2版更新:支持两个账号一键切换
    小程序支持打开APP了 还有小程序的标题栏也可以自定义
    小程序发布重磅数据:日活跃用户数1.7亿、已上线小程序58万个,覆盖100万开发者、2300个第三方平台
    张小龙2018PRO版微信公开课演讲全文 透露2018微信全新计划
    除了跳一跳还有16款微信小游戏可以玩
    小游戏里潜藏着600亿的大市场
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9328688.html
Copyright © 2011-2022 走看看