Mongo db,添加类似于SQL的SELECT *

问题描述 投票:0回答:1

我需要在mongo结果中添加一个“计算的”字段,但是投影会删除所有保留在转换后字段上的数据。

在mongo中有没有通往SELECT *, operation(cola,colB) AS NEW_COL FROM T的方法?

样本数据:

COL_A COL_B COL_C
a     1     1581292800    
b     2     1583884800
c     3     1616284800 

我想要一个保留第一列的查询,并用Unix时间添加COL_C

COl_A COL_B COL_C       COL_D       
a     1     1581292800  10/02/2020    
b     2     1583884800  11/03/2020  
c     3     1616284800  21/03/2021

在sql中,我将执行以下操作:

SELECT *, DATE(FROM_UNIXTIME(COL_C)) AS COL_D
FROM T;

在mongo中,我可以有一个带有转换字段的表,但是我无法获取所有未更改的数据

db.getCollection('T').aggregate([  
{  
    "$project":{
        "COL_D": {
            "$add": [new Date(0), "$COL_C" ]
                  }
               }
}
])

编辑:BSON数据

/*1*/
{
    "_id" : ObjectId("5ec61e577d30a51a840f564a"),
    "sessionId" : "20cbd618-e109-42be-aac6-1398284e9ef3",
    "timestamp" : NumberLong(1590042199043),
    "value": "hi"
    "events" : [ 
        {
            "eventType" : "startRecording",
            "timestamp2" : NumberLong(1590042198969),

        }, 
        {
            "eventType" : "stopRecording",
            "timestamp2" : NumberLong(1590042199572),

        }
    ],

    "updated" : true,

}

/*2*/
{
    "_id" : ObjectId("5ec61eca7d30a51a840f564c"),
    "sessionId" : "00000000-9D5850C1-E3E8-4EB7-8EBE-9E94E373CEBA",
    "timestamp" : NumberLong(1590042314163) /*COL_C*/
    "value": "lo"
    "events" : [ 
        {
            "eventType" : "start",
            "timestamp2" : NumberLong(1590042314134),

        }, 
        {
            "eventType" : "update",
            "timestamp2" : NumberLong(1590042316217),

        }, 
        {
            "eventType" : "stopRecording",
            "timestamp2" : NumberLong(1590042316234),

        }
    ]

    },
    "updated" : true,

}

预期的BSON:

/*1*/
{
    "_id" : ObjectId("5ec61e577d30a51a840f564a"),
    "sessionId" : "20cbd618-e109-42be-aac6-1398284e9ef3",
    "timestamp" : NumberLong(1590042199043), /*COL_C*/
    "value": "hi"
    "events" : [ 
        {
            "eventType" : "startRecording",
            "timestamp2" : NumberLong(1590042198969),

        }, 
        {
            "eventType" : "stopRecording",
            "timestamp2" : NumberLong(1590042199572),

        }
    ],

    "updated" : true,
    "COL_D" : ISODate("1970-01-01T00:00:00.000Z")
}

/*2*/
{
    "_id" : ObjectId("5ec61eca7d30a51a840f564c"),
    "sessionId" : "00000000-9D5850C1-E3E8-4EB7-8EBE-9E94E373CEBA",
    "timestamp" : NumberLong(1590042314163)
    "value": "lo"
    "events" : [ 
        {
            "eventType" : "start",
            "timestamp2" : NumberLong(1590042314134),

        }, 
        {
            "eventType" : "update",
            "timestamp2" : NumberLong(1590042316217),

        }, 
        {
            "eventType" : "stopRecording",
            "timestamp2" : NumberLong(1590042316234),

        }
    ]

    },
    "updated" : true,
    "COL_D" : ISODate("1970-01-01T00:00:00.000Z")
}
mongodb select aggregate
1个回答
0
投票

尝试这个:

db.collection.aggregate([
   {
      $addFields: {
         COL_D: new Date("$COL_C")
      }
   }
]) 

注意,在MongoDB / JavaScript中,Date的值以距1970-01-01 00:00:00ZMilli秒为单位,因此您可能必须使用new Date({ $multiply: [ "$COL_C", 1000 ] })

© www.soinside.com 2019 - 2024. All rights reserved.