简体   繁体   English

如何在php mongo查询中将unix时间戳转换为mongodb日期

[英]how to convert unix timestamp to mongodb date in php mongo query

What will be the equivalent mongo php query. 什么是等效的mongo php查询。 $time is filed of document having php unixtimestamp $ unixtimestamp提交文件的时间

db.collection.aggregate({$project : { year : {$year : ISODate("$time"*1000)} }})

Take this approach which adds the epoch date to the timestamp in milliseconds via the $add operator (which would be as a result of another $multiply operation on the time field by 1000 seconds) and the result will be the $year part of your date milliseconds since epoch: 采用这种方法,通过$add运算符将时间戳添加到时间戳(以毫秒为单位)(这将是时间字段上另一个$multiply操作的结果,持续1000秒),结果将是您日期的$year部分自纪元以来的毫秒数:

db.collection.aggregate([    
    {
        "$project": {
            "year": {
                "$year": {
                    "$add": [ new Date(0), { "$multiply": [ "$time", 1000 ] } ]
                }
            }
         } 
    }
]);

The equivalent PHP syntax would be something like: 等效的PHP语法类似于:

$collection->aggregate([
    [
        "$project" => [
            "year" => [
                "$year" => [
                    "$add": [ new MongoDate(0), [ "$multiply": [ "$time", 1000 ] ] ]
                ]
            ]
        ]
    ]
]);

What have you tried so far, are you using the php Mongo library? 你到目前为止尝试了什么,你使用的是php Mongo库吗?

Here an example with a timestamp: 这是一个带时间戳的示例:

$collection = $mongoClient->your_collection->your_type;
$pipeline = [
    [
        '$project' => [
            'year' => [
                '$year' => new \MongoDate($timestamp)
            ],
        ],
    ],
];

$results = $collection->aggregate($pipeline);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM