Friday, March 10, 2017

How to retrieve all records from nested array in mongodb

everyone, I want to retrieve all boards which have a specific owner name from the mongodb database. I am using Laravel

Here is the sample data.

{
        "_id" : ObjectId("58c2e3b853c06144b6038f3e"),
        "name" : "a new board",
        "description" : "fxcb",
        "owner" : {
                "id" : "58bc562553c06113f130bbb3",
                "name" : "Joker"
        },
        "slug" : "a_new_board",
        "updated_at" : ISODate("2017-03-10T17:34:48.628Z"),
        "created_at" : ISODate("2017-03-10T17:34:48.628Z")
}
{
        "_id" : ObjectId("58c3097b53c06144b6038f40"),
        "name" : "Board by Tom Cruise",
        "description" : "afgf",
        "owner" : {
                "id" : "58c3090b53c06144b6038f3f",
                "name" : "Tom Cruise"
        },
        "slug" : "board_by_tom_cruise",
        "updated_at" : ISODate("2017-03-10T20:15:55.132Z"),
        "created_at" : ISODate("2017-03-10T20:15:55.132Z")
}

What i have tried:

$boards = Board::where(['owner']['name'],'=', 'Joker');

gives the error

Undefined index: name

and $boards = Board::where(['owner']['name'],'=', 'Joker'); gives the error

Array to string conversion

Please help. Thank you in advance.



via Aatish Kumar

Advertisement