查找查询检索结果中的Mongoose格式datetime字段

时间:2017-10-13 08:00:31

标签: node.js mongodb mongoose nosql

需要使用Mongoose和node.js

格式化ISO日期时间值

MySQL表示例

table example

在MySQL中,我们可以使用select query本身格式化日期。以下示例供参考

SELECT DATE_FORMAT(created_at, "%d/%m/%Y") AS formatted_date FROM TABLE_NAME where share_count>1

MongoDB集合文档示例

/* 1 */
{
    "_id" : ObjectId("59ca6220f11b8212bc2c616f"),
    "created_at" : ISODate("2017-09-26T14:20:16.667Z"),
    "booking_key" : "7O5YQY990",
     share_count : 2
}

/* 2 */
{
    "_id" : ObjectId("59ca6625cbcb601a209eda57"),
    "created_at" : ISODate("2017-09-26T14:37:25.211Z"),
    "booking_key" : "3WW33HNO1",
     share_count : 3
}

/* 3 */
{
    "_id" : ObjectId("59ca6761cc4b29077c21f5cd"),
    "created_at" : ISODate("2017-09-26T14:42:41.725Z"),
    "booking_key" : "VT9QWWYO4",
     share_count : 0
}

需要知道如何像{MySQL}一样格式化created_at并检索结果集列表

Collection.find({ share_count: {'$gte':1}}, function(err, resultset) {
});

预期结果

/* 1 */
    {
        "_id" : ObjectId("59ca6220f11b8212bc2c616f"),
        "created_at" : "26/09/2017",
        "booking_key" : "7O5YQY990",
         share_count : 2
    }

    /* 2 */
    {
        "_id" : ObjectId("59ca6625cbcb601a209eda57"),
        "created_at" : "26/09/2017",
        "booking_key" : "3WW33HNO1",
         share_count : 3
    }

    /* 3 */
    {
        "_id" : ObjectId("59ca6761cc4b29077c21f5cd"),
        "created_at" : "09/09/2017",
        "booking_key" : "VT9QWWYO4",
         share_count : 0
    }

0 个答案:

没有答案