在MongoDB中的两个日期之间获取数据时获取空对象

发布于 2025-02-06 23:19:44 字数 3653 浏览 1 评论 0原文

我正在尝试使用php卷曲来获取mongoDB数据。我的代码就像下面。

            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));

我会像下面的


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
                )

        )
    )
)

一样获得输出

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));

输出

stdClass Object
(
    [documents] => Array
        (
        )

)

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
        (
        )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-02-13 23:19:45

没有逻辑的文档集可以符合此条件,因为其createon应该是 都大于或等于x,而小于x。
此查询将始终返回一个空集:

db.collection.find({
  createdOn: {
    $gte: ISODate("2022-04-11T00:08:54Z"),
    $lt: ISODate("2022-04-11T00:08:54Z")
  }
})

即使为此文档:

  {
    createdOn: ISODate("2022-04-11T00:08:54Z"),
    a: 2
  }

mongodb Playground 上检查它。

There is no logical set of documents that can fit this condition as its createdOn should be both greater than or equal to X and less than X.
This query will always return an empty set:

db.collection.find({
  createdOn: {
    $gte: ISODate("2022-04-11T00:08:54Z"),
    $lt: ISODate("2022-04-11T00:08:54Z")
  }
})

Even for this document:

  {
    createdOn: ISODate("2022-04-11T00:08:54Z"),
    a: 2
  }

Check it on mongoDB playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文