Getting empty object while fetching data between two Dates

I am trying to fetch MongoDB data using CURL with PHP. My code is like below.

            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->apiPath . $action,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                                        "dataSource": "' . $this->dataSource . '",
                                        "database": "' . $this->database . '",
                                        "collection": "' . $this->collection . '",
                                        "filter": { 
                                            "type": { "$eq": "'.$query.'" }                                            
                                        }
                                    }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Access-Control-Request-Headers: *',
                    'api-key: ' . $this->apiKey,
                ),
            ));
            $mongodata = json_decode(curl_exec($curl));

I am getting output like below

stdClass Object
(
    [documents] => Array
    (
        [0] => stdClass Object
        (
            [_id] => 6253c5fc22d6ee83815a111a
            [lang] => Bangla
            [type] => top
            [createdAt] => 2022-04-11T00:08:54Z
            [searchTerm] => key
            [customerId] => 747498
            [wpId] => 20
            [results] => stdClass Object
                (
                    [parts] => 0
                    [pages] => 0
                    [productCategory] => 0
                )

        )
    )
)

But if I use below code

curl_setopt_array($curl, array(
                CURLOPT_URL => $this->apiPath . $action,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                                        "dataSource": "' . $this->dataSource . '",
                                        "database": "' . $this->database . '",
                                        "collection": "' . $this->collection . '",
                                        "filter": { 
                                            "type": { "$eq": "'.$query.'" },
                                            "createdOn": {
                                                "$gte": "2022-04-11T00:08:54Z",
                                                "$lt": "2022-04-11T00:08:54Z"
                                            } 
                                        }
                                    }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Access-Control-Request-Headers: *',
                    'api-key: ' . $this->apiKey,
                ),
            ));
            $mongodata = json_decode(curl_exec($curl));

I am getting empty object like below

stdClass Object
(
    [documents] => Array
        (
        )

)

Try with

{ "$date" : "2022-04-11T00:08:54Z" }

rather than

The types must match.

1 Like

Thanks @steevej . I am using below code.

curl_setopt_array($curl, array(
                CURLOPT_URL => $this->apiPath . $action,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                                        "dataSource": "' . $this->dataSource . '",
                                        "database": "' . $this->database . '",
                                        "collection": "' . $this->collection . '",
                                        "filter": { 
                                            "type": { "$eq": "'.$query.'" },
                                            "createdOn": {
                                                "$date" : "2022-04-11T00:08:54Z"
                                            } 
                                        }
                                    }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Access-Control-Request-Headers: *',
                    'api-key: ' . $this->apiKey,
                ),
            ));
            $mongodata = json_decode(curl_exec($curl));

But I am getting empty object. Thanks.

According to Date equality in Data API - #13 by Jason_Tran it should work.

And then right after I wrote the above and I go check and I found that you are not using the correct field name for your date query.

but you query

1 Like

Thanks @steevej . Your solution is working. But I would like to apply a Date range (Start Date & End Date ) to fetch items. Could you please help me in this regard ?

It is the same thing. Your date range query was also using the wrong field name.

1 Like

Thanks @steevej . Should I use createdOn ? or anything else ?

I wrote that you are

and posted your code that uses

which produce no results.

So to answer

of course NOT. This is the

And to answer

The answer is that yes. You have to use the correct field name rather than the wrong field name. And the correct field name is one present in your data. It is:

Just like you did earlier

1 Like

Thanks @steevej . I tried your solution before. I am explaining everything once again. I have below item in MongoDB.

stdClass Object
(
    [documents] => Array
    (
        [0] => stdClass Object
        (
            [_id] => 6253c5fc22d6ee83815a111a
            [lang] => Bangla
            [type] => top
            [createdAt] => 2022-04-11T00:08:54Z
            [searchTerm] => key
            [customerId] => 747498
            [wpId] => 20
            [results] => stdClass Object
                (
                    [parts] => 0
                    [pages] => 0
                    [productCategory] => 0
                )

        )
    )
)

I am using below code as per your instruction.

curl_setopt_array($curl, array(
                CURLOPT_URL => $this->apiPath . $action,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                                        "dataSource": "' . $this->dataSource . '",
                                        "database": "' . $this->database . '",
                                        "collection": "' . $this->collection . '",
                                        "filter": { 
                                            "type": { "$eq": "'.$query.'" },
                                            "createdAt": {
                                                "$gte": "2022-04-11T00:08:54Z",
                                                "$lt": "2022-04-11T00:08:54Z"
                                            }
                                        }
                                    }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Access-Control-Request-Headers: *',
                    'api-key: ' . $this->apiKey,
                ),
            ));
            $mongodata = json_decode(curl_exec($curl));

But I am getting empty object like below.

stdClass Object
(
    [documents] => Array
        (
        )

)

Thanks @steevej .

Good we are making progress.

You have now the correct field name in your query. But you still have the wrong type. You are passing dates as string data rather that dates as date data. You are still missing the part of my answer that is in the first reply I made:

This makes sure that you string date is converted to a date object.

Thanks @steevej . I have to pass 2 Dates (Start Date & End Date). I am using below code now.

curl_setopt_array($curl, array(
                CURLOPT_URL => $this->apiPath . $action,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                                        "dataSource": "' . $this->dataSource . '",
                                        "database": "' . $this->database . '",
                                        "collection": "' . $this->collection . '",
                                        "filter": { 
                                            "type": { "$eq": "'.$query.'" },
                                            "createdAt": {
                                                "$gte": { "$date" : "2022-04-11T00:08:54Z" },
                                                "$lt": { "$date" : "2022-04-11T00:08:54Z" }
                                            }
                                        }
                                    }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Access-Control-Request-Headers: *',
                    'api-key: ' . $this->apiKey,
                ),
            ));
            $mongodata = json_decode(curl_exec($curl));

But I am still getting empty object.

stdClass Object
(
    [documents] => Array
        (
        )

)

Thanks.

Of course you do.

Think about it, there is no way createdAt can be both $gte a given date and $lt than the same given date.

The highest date $lt 2022-04-11T00:08:54Z has to be 2022-04-11T00:08:53Z, definitively not

1 Like