在MongoDB中的两个日期之间获取数据时获取空对象
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有逻辑的文档集可以符合此条件,因为其
createon
应该是 都大于或等于x,而小于x。此查询将始终返回一个空集:
即使为此文档:
在 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:
Even for this document:
Check it on mongoDB playground