Mongodb concat int 和 string [英] Mongodb concat int and string

查看:23
本文介绍了Mongodb concat int 和 string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我收藏中的所有文件投影 FileName 和 FileSize,大小为 50 mb 或更大,但我无法连接 FileSize 类型,因为它具有 Int 类型

I'm trying to project FileName and FileSize for all my files in my collection with a size of 50 mb and greater, but I cannot concat a the type FileSize as it has a type of Int

我希望投影是

{
"result" : [ 
    {
        "_id" : ObjectId("5652c399a21dad0bb01b6308"),
        "FileName" : "1234567890.xml",
        "FileSize" : "11.06 MB"
    }, 
    {
        "_id" : ObjectId("5652c399a21dad0bb01b630f"),
        "FileName" : "2468101214.xml",
        "FileSize" : "320.48 MB"
    }, 
    {
        "_id" : ObjectId("5652c399a21dad0bb01b631f"),
        "FileName" : "3691215180.xml",
        "FileSize" : "12.95 MB"
    }
}

但到目前为止我只能返回以下内容

But so far I can Only return the following

{
"result" : [ 
    {
        "_id" : ObjectId("5652c399a21dad0bb01b6308"),
        "FileName" : "1234567890.xml",
        "FileSize" : 11.0610504150390630
    }, 
    {
        "_id" : ObjectId("5652c399a21dad0bb01b630f"),
        "FileName" : "2468101214.xml",
        "FileSize" : 320.4827098846435500
    }, 
    {
        "_id" : ObjectId("5652c399a21dad0bb01b631f"),
        "FileName" : "3691215180.xml",
        "FileSize" : 12.9519605636596680
    }
}

我的查询:

    db.MyCollection.aggregate(

  // Pipeline
  [
    // Stage 1
    {
      $match: {
      FileSize: {$gte: 5000000}
      }
    },
    // Stage 2
    {
      $project: {
        FileName: 1,
        FileSize: {$divide: ["$FileSize", 1048576]}
      }
    },
    // Stage 3
    {
        $project:{
            FileName:1,
            FileSize:{$concat:["$FileSize", "MB"]}
        }
     }

如何连接 FileSize 和MB"字段?

How do I concat the FileSize and "MB" field?

推荐答案

这里的技巧是使用 $substr 转换为字符串,还有一些额外的处理小数点精度:

The trick here is to use $substr to do the conversion to a string, and a few little extras to handle the decimal point precision:

    { "$project": {
        "FileName": 1,
        "FileSize": {
            "$concat": [
                { "$substr": [
                    { "$subtract": [ "$FileSize", { "$mod": [ "$FileSize", 1 ] }]},
                    0,
                    -1
                ]},
                { "$substr": [ { "$mod": [ "$FileSize", 1 ] }, 1, 3] },
                " MB",
            ]
        }
    }}

或者更好的是,在 $let 在 MongoDB 2.6 之后的版本中,或者如果需要的话.单个管道阶段比两个更有效:

Or better yet, combine into a single $project, either with the help of $let in versions later than MongoDB 2.6, or in long for if needed. A single pipeline stage is more efficient than two:

    { "$project": {
        "FileName": 1,
        "FileSize": {
            "$let": {
                "vars": {
                    "FileSize": { "$divide": [ "$FileSize", 1048576 ] }
                },
                "in":{
                    "$concat": [
                        { "$substr": [
                            { "$subtract": [ "$$FileSize", { "$mod": [ "$$FileSize", 1 ] }]},
                            0,
                            -1
                        ]},
                        { "$substr": [ { "$mod": [ "$$FileSize", 1 ] }, 1, 3] },
                        " MB",
                    ]
                }
            }
        }
    }}

所以只要你在小数点处分解数字(通过 $mod )你可以向字符串的其余部分抛出一个长度"参数来处理任意长度的数字.用$mod分隔余数",字符串长度到小数点后两位总是三",当然是从第二个位置开始,以跳过开头的0.

So as long as you break up the number at the decimal point ( via $mod ) you can throw a "length" argument to the rest of the string to deal with numbers of arbitary length. With the "remainder" separated from the use of $mod the length of the string to two decimal places is always "three", starting of course from the second position in order to skip the leading 0.

准确返回您所要求的内容:

Returns exactly what you asked for:

{
        "_id" : ObjectId("5652c399a21dad0bb01b6308"),
        "FileName" : "1234567890.xml",
        "FileSize" : "11.06 MB"
}
{
        "_id" : ObjectId("5652c399a21dad0bb01b630f"),
        "FileName" : "2468101214.xml",
        "FileSize" : "320.48 MB"
}
{
        "_id" : ObjectId("5652c399a21dad0bb01b631f"),
        "FileName" : "3691215180.xml",
        "FileSize" : "12.95 MB"
}

这篇关于Mongodb concat int 和 string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆