将 ISO 日期转换为 yyyy-mm-dd 格式 [英] Convert ISO date to yyyy-mm-dd format

查看:32
本文介绍了将 ISO 日期转换为 yyyy-mm-dd 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定集合(#name: users) 结构:

Given collection(#name: users) Structure:

{
"_id" : ObjectId("57653dcc533304a40ac504fc"),
"username" : "XYZ",
"followers" : [ 
    {
        "count" : 31,
        "ts" : ISODate("2016-06-17T18:30:00.996Z")
    }, 
    {
        "count" : 31,
        "ts" : ISODate("2016-06-18T18:30:00.288Z")
    }
]
}

我想根据用户名字段查询此集合,并以yyyy-mm-dd"格式返回 ts.预期输出:

I want to query this collection based on username field, and ts to be returned in 'yyyy-mm-dd' format. Expected Output:

{
"_id" : ObjectId("57653dcc533304a40ac504fc"),
"username" : "XYZ",
"followers" : [ 
    {
        "count" : 31,
        "date" : "2016-06-17"
    }, 
    {
        "count" : 31,
        "date" : "2016-06-18"
    }
]
}

我尝试过这样的事情:

db.users.aggregate([
{$match:{"username":"xyz"}},
{$project:{ "followers":{"count":1,
        "date":"$followers.ts.toISOString().slice(0,10).replace(/-/g,'-')"
          }}
}
])

但它似乎不起作用.有人可以帮忙吗?非常感谢.

But it doesn't seems to be working. Can anyone please help? Thanks much.

推荐答案

考虑运行一个聚合管道,它允许您先展平数据列表,然后使用 $dateToString 运算符,然后将扁平化的文档重新组合为得到你想要的结果.

Consider running an aggregation pipeline that will allow you to flatten the data list first, project the new field using the $dateToString operator, then regroup the flattened docs to get your desired result.

以上内容可以显示在三个不同的管道中:

The above can be shown in three distinct pipelines:

db.users.aggregate([
    { "$match": { "username": "xyz" } },
    { "$unwind": "$followers" },
    {
        "$project": {
            "username": 1,
            "count": "$followers.count",
            "date": { "$dateToString": { "format": "%Y-%m-%d", "date": "$followers.ts" } }
        }
    },
    {
        "$group": {
            "_id": "$_id",
            "username": { "$first": "$username" },
            "followers": { "$push": {
                "count": "$count",
                "date": "$date"
            }}
        }
    }
])

<小时>

在 MongoDB 3.4 及更新版本中,您可以使用新的 $addFields 管道步骤与 $map 无需展开和分组即可创建数组字段:


With MongoDB 3.4 and newer, you can use the new $addFields pipeline step together with $map to create the array field without the need to unwind and group:

db.users.aggregate([
    { "$match": { "username": "xyz" } },    
    {
        "$addFields": {
            "followers": { 
                "$map": { 
                    "input": "$followers", 
                    "as": "follower",
                    "in": { 
                        "count": "$$follower.count", 
                        "date": { 
                            "$dateToString": { 
                                "format": "%Y-%m-%d", 
                                "date": "$$follower.ts" 
                            }
                        } 
                    } 
                } 
            }
        }
    }
])

这篇关于将 ISO 日期转换为 yyyy-mm-dd 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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