如何找到匹配嵌套键laravel mongodb jenssegers的记录

时间:2017-10-27 08:48:40

标签: php mongodb laravel jenssegers-mongodb

我在laravel中使用jenssegers mongodb包来查询mongodb。如何在下面的json文档中检索仅匹配游戏作为板球的记录。

      {
       "_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"
            }
]
       }

    }

2 个答案:

答案 0 :(得分:1)

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

这样的事情应该起作用

答案 1 :(得分:1)

您可以将whereRaw方法与elemMatch结合使用:

DB::collection('users')->where(
    'Personal_info.hobbies',
    'elemMatch',
    [ 'games' => 'cricket' ]
)->get()