I Aggregation Rahmen Pipeline mit Mongo Mapper in der Transaktionstabelle verklagen will, sollte das Ergebnis so etwas wie diese: I 7000 Transaktionen in einer Sammlung Angenommen habe mit status 1,2,3,4 (1=pending, 2=confirmed, 3=decline, 4=claimed)
php f3 Rahmen Verwendung Aggregation Rahmen Pipeline mit Mongo Mapper
Data Representation:
Pending Count | Confirmed Count | Decline Count | Claimed Count
4000 2000 1000 0
1. Das Problem ist, ich bin nicht sicher, welches Feld sollte für den Schlüssel in der Gruppe verwendet werden.
- Mein Code gibt mir alle Datensätze zurück, die den Kriterien entsprechen.
bitte einen Blick auf meinen Code haben:
$filters = array('dateCreated'=>array(
'$gte'=>'2015-01-01',
'$lte'=>'2017-05-15'
));
$group = [
'keys' =>['userId'=>1],
'initial' => ['pendingCount'=>0, 'confirmedCount'=>0, 'declineCount'=>0, 'claimedCount'=>0],
'reduce'=>'
function(obj, result){
switch(result.status){
case 1:
result.pendingCount++;
break;
case 2:
result.confirmedCount++;
break;
case 3:
result.declineCount++;
break;
case 4:
result.claimedCount++;
break;
}
}
',
'finalize'=>'function(result){}'
];
$data = $this->mapper->find($filter, ['group'=>$group]);
echo "data<pre>";
print_r($data);
Ausgang:
Hier sehen Sie es mir mehr als ein Dokumenten: Rückkehr aber ich will nur einen Datensatz für alle Transaktionen, Schlüssel sein kann, userId
sollte etwas ersetzt werden ‚null`
Array ( [0] => Array ( [_id] => MongoId Objekt ( [$ id] => 591a8f5bb06b243d0262d0a1 )
[userId] => [pendingCount] => 0 [confirmedCount] => 0 [declineCount] => 0 [claimedCount] =>0 ) [1] => Array ( [_id] => MongoId Object ( [$id] => 591a8f5bb06b243d0262d0a2 ) [userId] => [pendingCount] => 0 [confirmedCount] => 0 [declineCount] => 0 [claimedCount] =>0 ) [2] => Array ( [_id] => MongoId Object ( [$id] => 591a8f5bb06b243d0262d0a3 ) [userId] => 38730976 [pendingCount] => 0 [confirmedCount] => 0 [declineCount] => 0 [claimedCount] =>0 ) [3] => Array ( [_id] => MongoId Object ( [$id] => 591a8f5bb06b243d0262d0a4 ) [userId] => 64920 [pendingCount] => 0 [confirmedCount] => 0 [declineCount] => 0 [claimedCount] =>0 )
....
Hallo, ich ersetzt Schlüssel mit '[]' und Fest Typo 'obj.status' jetzt nur einen Datensatz mit dieser Ausgabe bekommen:' Array ( [0] => Array ( [_id] => MongoId Objekt ( [$ id] => 591aa40ab06b244160a4101d ) [endingCount] => 0 \t [confirmedCount] => 0 \t [declineCount ] => 0 \t [disposedCount] => 0 ) ) 'Ich denke, Problem ist mit switch-Anweisung –
Ich bin sicher, es gibt Datensätze mit unterschiedlichem Status. –
jetzt, es funktioniert, ich habe gerade doppelte Anführungszeichen in 'case" 1 "hinzugefügt: ...' aber ich bin mir nicht sicher, warum MongoId Object ist da? –