【问题标题】:Is there any way to get date from ObjectId from mongoose using aggregate?有没有办法使用聚合从猫鼬中获取 ObjectId 的日期?
【发布时间】:2019-07-29 15:47:34
【问题描述】:

我有Users 收藏。 devices 都在对象数组中。

[{
    "_id" : ObjectId("5c66a979e109fe0f537c7e37"),
    "devices": [{
            "dev_token" : "XXXX",
            "_id" : ObjectId("5ccc0fa5f7778412173d22bf")
        }]

},{
    "_id" : ObjectId("5c66b6382b18fc4ff0276dcc"),
    "devices": [{
            "dev_token" : "XXXX",
            "_id" : ObjectId("5c93316cc33c622bdcfaa4be")
        }]

}]

我需要通过在devices 中添加新字段date 来查询文档

"devices": [{
       "dev_token" : "XXXX",
        "_id" : ObjectId("5c93316cc33c622bdcfaa4be"),
       "date": ISODate("2012-10-15T21:26:17Z")
}]

date 来自devices._id.getTimestamp() 的密钥

我尝试使用聚合这个,不知道如何使用getTimestamp()

db.getCollection('users').aggregate([ {
    "$unwind": "$devices"
}, {
    "$group": {
        "_id": "$_id",
        "devices": {
                "$push": "$devices._id.getTimestamp()"
          }
     }
}])

我使用$devices._id.getTimestamp(),这可能是错误的..这​​是我如何处理这个..感谢您的提前

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query


    【解决方案1】:

    您可以使用$toDate 从_id 字段中获取时间戳。

    date 字段添加到unwind 阶段之后的每个devices 元素,使用$addFields

    试试这个:

    db.getCollection('users').aggregate([ {
        "$unwind": "$devices"
    },{
        $addFields : {
            "devices.date": { $toDate: "$_id" }
        }
    }, {
        "$group": {
            "_id": "$_id",
            "devices": {
                    "$push": "$devices"
              }
         }
    }])
    

    您可以在Mongo Playground查看结果(只需按“运行”)

    【讨论】:

    • 是的,但 $toDate 在 mongodb >4.0 中。不能在 MongoDB 3.6 版本中使用。还有其他选择吗?
    • 我试图找到一种可用于 MongoDB 3.6 的方法,但我找不到任何方法。如果我发现了什么,我会看更多并更新答案。
    • 感谢您的回复。如果没有办法,就在js里做
    【解决方案2】:

    使用 MongoDb 3.6

    $dateFromParts 运算符在这里派上用场,您可以将它与其他日期运算符结合使用。你不需要 到$unwind 数组,因为您可以使用$map 映射devices 数组文档并使用上述表达式添加额外的日期字段。

    这可以跟下面的示例管道:

    db.getCollection('users').aggregate([    
        { "$addFields": {
            "devices": {
                "$map": {
                    "input": "$devices",
                    "in": {
                        "dev_token": "$$this.dev_token",
                        "_id": "$$this._id",
                        "date": {
                            "$dateFromParts": {
                                'year': { "$year": "$$this._id"}, 
                                'month': { "$month": "$$this._id"}, 
                                'day':{ "$dayOfMonth": "$$this._id"},
                                'hour': { "$hour": "$$this._id"}, 
                                'minute': { "$minute": "$$this._id"}, 
                                'second': { "$second": "$$this._id"},
                                'millisecond': { "$millisecond": "$$this._id"}
                            }
                        }
                    }
                }
            }
        } }
    ])
    

    输出

    /* 1 */
    {
        "_id" : ObjectId("5c66a979e109fe0f537c7e37"),
        "devices" : [ 
            {
                "dev_token" : "XXXX",
                "_id" : ObjectId("5ccc0fa5f7778412173d22bf"),
                "date" : ISODate("2019-05-03T09:53:41.000Z")
            }
        ]
    }
    
    /* 2 */
    {
        "_id" : ObjectId("5c66b6382b18fc4ff0276dcc"),
        "devices" : [ 
            {
                "dev_token" : "XXXX",
                "_id" : ObjectId("5c93316cc33c622bdcfaa4be"),
                "date" : ISODate("2019-03-21T06:38:36.000Z")
            }
        ]
    }
    

    使用 MongoDb 4.0 及更新版本:

    可以稍微调整管道以使用新的 $toDate$convert 运算符。它们各自的用途如下:

    $toDate

    db.getCollection('users').aggregate([    
        { "$addFields": {
            "devices": {
                "$map": {
                    "input": "$devices",
                    "in": {
                        "dev_token": "$$this.dev_token",
                        "_id": "$$this._id",
                        "date": { "$toDate": "$$this._id" }
                    }
                }
            }
        } }
    ])
    

    $convert

    db.getCollection('users').aggregate([    
        { "$addFields": {
            "devices": {
                "$map": {
                    "input": "$devices",
                    "in": {
                        "dev_token": "$$this.dev_token",
                        "_id": "$$this._id",
                        "date": { 
                            "$convert": { "input": "$$this._id", "to": "date" }
                        }
                    }
                }
            }
        } }
    ])
    

    【讨论】:

      猜你喜欢
      • 2020-07-25
      • 2020-01-11
      • 1970-01-01
      • 2022-01-04
      • 2015-07-27
      • 2018-04-12
      • 2012-07-21
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多