0

I am using jenssegers mongodb package in laravel for querying mongodb. How to retrieve the records matching only the games as cricket in the below json document.

      {
       "_id": ObjectId("53402597d852426020000002"),
       "contact": "987654321",
       "dob": "01-01-1991",
       "gender": "M",
       "name": "Tom Benzamin",
       "user_name": “tombenzamin”,   
       “Personal_info”:[
            hobbies:{
                "games": "cricket",
                "favfilms": "lotr",
                "favfood": "burger"
            }
]
       }

    }
oldrock
  • 841
  • 2
  • 13
  • 26

2 Answers2

2
$crickets = DB::collection('games')->where('Personal_info.hobbies.games', 'cricket')->get();

something like this should work

ariawan
  • 198
  • 1
  • 8
2

You can use whereRaw method combined with elemMatch:

DB::collection('users')->where(
    'Personal_info.hobbies',
    'elemMatch',
    [ 'games' => 'cricket' ]
)->get()
krisanalfa
  • 6,268
  • 2
  • 29
  • 38