Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregate with ISODate #258

Open
rrr-bot opened this issue Jun 29, 2018 · 1 comment
Open

aggregate with ISODate #258

rrr-bot opened this issue Jun 29, 2018 · 1 comment
Labels
status:to be verified Needs to be reproduced and validated.

Comments

@rrr-bot
Copy link

rrr-bot commented Jun 29, 2018

What steps will reproduce the problem?

This is my raw code:

$query = [];

        $query[] = [
            '$match' =>
            [
                "updatedAt" => [
                    '$gte' => new \MongoDB\BSON\UTCDateTime()
                ]
            ]
        ];

        $query[] = [
            '$unwind' => '$messages'
        ];

        $query[] = [
            '$match' =>
            [
                "messages.publicMessage.time" =>
                [
                    '$gte' => strtotime('-1 day') * 1000
                ],
            ]
        ];

        $messages = Yii::$app->mongodb->createCommand()->aggregate(Queue::collectionName(), $query);

which generates bad query with backslashed ISODate:

dev.command({"aggregate":"queues","pipeline":[{"$match":{"updatedAt":{"$gte":"ISODate(\"2018-06-28T23:23:00.000Z\")"}}},{"$unwind":"$messages"},{"$match":{"messages.publicMessage.time":{"$gte":1530176891000}}}],"allowDiskUse":false,"cursor":{}})

Other atempts:

  1. UTCDAteTime
 "updatedAt" => [
                        '$gte' => new MongoDB\BSON\UTCDateTime(strtotime('-1 day'))
                    ]

gives:

dev.command({"aggregate":"queues","pipeline":[{"$match":{"updatedAt":{"$gte":"MongoDB\\BSON\\UTCDateTime(1530177111)"}}},{"$unwind":"$messages"},{"$match":{"messages.publicMessage.time":{"$gte":1530177111000}}}],"allowDiskUse":false,"cursor":{}})

also bad - with class name and also backslashed

  1. (new \MongoDB\BSON\UTCDateTime(strtotime('-1 day')))->toDateTime()
dev.command({"aggregate":"queues","pipeline":[{"$match":{"updatedAt":{"$gte":{"date":"1970-01-18 17:02:57.308000","timezone_type":1,"timezone":"+00:00"}}}},{"$unwind":"$messages"},{"$match":{"messages.publicMessage.time":{"$gte":1530177308000}}}],"allowDiskUse":false,"cursor":{}})

still bad - not compatible with mongo date or $dateToString

What's expected?

dev.command({"aggregate":"queues","pipeline":[{"$match":{"updatedAt":{"$gte":ISODate("2018-02-09T05:00:00Z")}}},{"$unwind":"$messages"},{"$match":{"messages.publicMessage.time":{"$gte":1530176891000}}}],"allowDiskUse":false,"cursor":{}})

What do you get instead?

dev.command({"aggregate":"queues","pipeline":[{"$match":{"updatedAt":{"$gte":"ISODate(\"2018-06-28T23:23:00.000Z\")"}}},{"$unwind":"$messages"},{"$match":{"messages.publicMessage.time":{"$gte":1530176891000}}}],"allowDiskUse":false,"cursor":{}})

Additional info

Q A
Yii version 2.0.15.1
Yii MongoDB version 2.1.7
MongoDB server version 3.6.4
PHP version 7.2.1
Operating system Debian 9
@needrunning
Copy link

当使用时间作为match条件时,一般使用范围查询,并且使用UTCDatetime格式。

@samdark samdark added the status:to be verified Needs to be reproduced and validated. label Jun 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated.
Projects
None yet
Development

No branches or pull requests

3 participants