如何 return 仅匹配数组项与 MongoDB PHP 库
How to return only matching arrays item with MongoDB PHP Library
我是 MongoDB 的新手,目前我正在使用 MongoDB 托管在 Atlas。
我的代码使用 PHP MongoDB 库。
数据库中有一个名为"transactions"的集合,其结构如下:
[
{
"product":"X",
"customer":"John",
"history":[
{
"payment_date":"2017-07-01",
"status":"paid"
},
{
"payment_date":"2017-08-02",
"status":"paid"
},
{
"payment_date":"2017-09-03",
"status":"paid"
}
]
},
{
"product":"Y",
"customer":"Mary",
"history":[
{
"payment_date":"2017-07-01",
"status":"paid"
},
{
"payment_date":"2017-08-02",
"status":"paid"
},
{
"payment_date":"2017-09-03",
"status":"paid"
}
]
}
]
基于这个答案,我有以下代码试图找到 payment_date 等于 2017-08-02
的历史记录
$manager = new \MongoDB\Driver\Manager(getenv('MONGODB_DSN'));
$db = new \MongoDB\Database($manager, getenv('MONGODB_DATABASE'));
$cursor = $db->transaction->find(
['history.payment_date' => '2017-08-02'],
['_id' => 0, 'history' => ['$elemMatch' => ['history.payment_date' => '2017-08-02']]]
);
return $cursor->toArray();
但是returns没有物品!
我在 MongoShell 中尝试了代码,它有效!
我还尝试了一些使用聚合的代码:
$manager = new \MongoDB\Driver\Manager(getenv('MONGODB_DSN'));
$db = new \MongoDB\Database($manager, getenv('MONGODB_DATABASE'));
$cursor = $db->transaction->aggregate(
[
[
'$match' => [
'history.payment_date' => "2017-08-02"
]
],
[
'$project' => [
'_id' => 0,
'history' => 1
]
]
]
);
return $cursor->toArray();
我关注了很多 link,但我没有成功!
所以如果你不给我另一个我将不胜感激link,因为我已经访问了数百个!
我会试一试:
$cursor = $db->transaction->aggregate(
[
[
'$match' => [
'history.payment_date' => "2017-08-02" // limit returned documents to ones with matches
]
],
[
'$unwind' => "$history" // split each document into multiple documents based on history
],
[
'$match' => [
'history.payment_date' => "2017-08-02" // only return documents that match
]
]
]
);
我是 MongoDB 的新手,目前我正在使用 MongoDB 托管在 Atlas。
我的代码使用 PHP MongoDB 库。
数据库中有一个名为"transactions"的集合,其结构如下:
[
{
"product":"X",
"customer":"John",
"history":[
{
"payment_date":"2017-07-01",
"status":"paid"
},
{
"payment_date":"2017-08-02",
"status":"paid"
},
{
"payment_date":"2017-09-03",
"status":"paid"
}
]
},
{
"product":"Y",
"customer":"Mary",
"history":[
{
"payment_date":"2017-07-01",
"status":"paid"
},
{
"payment_date":"2017-08-02",
"status":"paid"
},
{
"payment_date":"2017-09-03",
"status":"paid"
}
]
}
]
基于这个答案,我有以下代码试图找到 payment_date 等于 2017-08-02
的历史记录$manager = new \MongoDB\Driver\Manager(getenv('MONGODB_DSN'));
$db = new \MongoDB\Database($manager, getenv('MONGODB_DATABASE'));
$cursor = $db->transaction->find(
['history.payment_date' => '2017-08-02'],
['_id' => 0, 'history' => ['$elemMatch' => ['history.payment_date' => '2017-08-02']]]
);
return $cursor->toArray();
但是returns没有物品!
我在 MongoShell 中尝试了代码,它有效!
我还尝试了一些使用聚合的代码:
$manager = new \MongoDB\Driver\Manager(getenv('MONGODB_DSN'));
$db = new \MongoDB\Database($manager, getenv('MONGODB_DATABASE'));
$cursor = $db->transaction->aggregate(
[
[
'$match' => [
'history.payment_date' => "2017-08-02"
]
],
[
'$project' => [
'_id' => 0,
'history' => 1
]
]
]
);
return $cursor->toArray();
我关注了很多 link,但我没有成功! 所以如果你不给我另一个我将不胜感激link,因为我已经访问了数百个!
我会试一试:
$cursor = $db->transaction->aggregate(
[
[
'$match' => [
'history.payment_date' => "2017-08-02" // limit returned documents to ones with matches
]
],
[
'$unwind' => "$history" // split each document into multiple documents based on history
],
[
'$match' => [
'history.payment_date' => "2017-08-02" // only return documents that match
]
]
]
);