hello Sir,
i have few problems, would you like to feedback for this?
one of the data set is
{
"_id" : ObjectId("5ba4d1ec44e2c511f4572d04"),
"teamName" : "Team 1",
"details" : {
"bowlers" : [
{
"id" : 1537528330,
"first_name" : "wdfvdv",
"last_name" : "wdfvwdfv",
"age" : 34,
"type" : "bowlers",
"team" : "Team 1"
}
],
"batsmans" : [
{
"id" : 1537531253,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537532793,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537532886,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537533057,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537533241,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537533450,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537533786,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537534126,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537534293,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537534311,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537534324,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537534335,
"first_name" : "first",
"last_name" : "batsman",
"age" : 23,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537534344,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
},
{
"id" : 1537536125,
"first_name" : null,
"last_name" : null,
"age" : 0,
"type" : "batsmans",
"team" : "Team 1"
}
]
},
"updated_at" : ISODate("2018-09-21T11:11:40.000Z"),
"created_at" : ISODate("2018-09-21T11:11:40.000Z")
}
Actually, i am trying to get only those player list who has more then 20 age.. but got extra players who has age 0 also... nothing else.. have sen your test file, query builder file... but didn't get anything as expected
the query is:
$z = Cricket::where('details'.'.'.$type, 'elemMatch', ['age'=> ['$gt' => 20]])->get()->toArray();
NB: $type is either batsman or bowler. how can i check the players from all the keys under details.
this is the output i got
Array
(
[0] => Array
(
[_id] => 5ba4d1ec44e2c511f4572d04
[teamName] => Team 1
[details] => Array
(
[bowlers] => Array
(
[0] => Array
(
[id] => 1537528330
[first_name] => wdfvdv
[last_name] => wdfvwdfv
[age] => 34
[type] => bowlers
[team] => Team 1
)
)
[batsmans] => Array
(
[0] => Array
(
[id] => 1537531253
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[1] => Array
(
[id] => 1537532793
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[2] => Array
(
[id] => 1537532886
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[3] => Array
(
[id] => 1537533057
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[4] => Array
(
[id] => 1537533241
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[5] => Array
(
[id] => 1537533450
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[6] => Array
(
[id] => 1537533786
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[7] => Array
(
[id] => 1537534126
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[8] => Array
(
[id] => 1537534293
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[9] => Array
(
[id] => 1537534311
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[10] => Array
(
[id] => 1537534324
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[11] => Array
(
[id] => 1537534335
[first_name] => first
[last_name] => batsman
[age] => 23
[type] => batsmans
[team] => Team 1
)
[12] => Array
(
[id] => 1537534344
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
[13] => Array
(
[id] => 1537536125
[first_name] =>
[last_name] =>
[age] => 0
[type] => batsmans
[team] => Team 1
)
)
)
[updated_at] => 2018-09-21 11:11:40
[created_at] => 2018-09-21 11:11:40
)
[1] => Array
(
[_id] => 5ba4d20f44e2c511f30beae5
[teamName] => Team 2
[details] => Array
(
[bowlers] => Array
(
)
[batsmans] => Array
(
[0] => Array
(
[id] => 1537528343
[first_name] => wdfvwdfv
[last_name] => wvfwdfc
[age] => 45
[type] => batsmans
[team] => Team 2
)
[1] => Array
(
[id] => 1537528354
[first_name] => dfvwdfvwd
[last_name] => wdfvwdfwdc
[age] => 67
[type] => batsmans
[team] => Team 2
)
)
)
[updated_at] => 2018-09-21 11:12:15
[created_at] => 2018-09-21 11:12:15
)
)
Try use $elemMatch in projection
$z = Cricket::where('details'.'.'.$type, 'elemMatch', ['age'=> ['$gt' => 20]])->get(['details'.''.$type =>['$elemMatch' => ['age' =>['$gt' => 20]] ]])->toArray();
Most helpful comment
Try use
$elemMatchin projection