Search code examples
laravelmongodblaravel-6.2

How to query- Where In Array Key Value Laravel with Mongodb


This is my database Structure

{ 
    "_id":"5e4d4d6fc01c0000cc0009bd",
    "UID":"5e30376327050000b3006cde",
    "delevery_charge":25,
    "order_item":[ 
        { 
            "id":"5e2f4a636c630000f20053bc",
            "Name":"XXXXX",
            "info":"5e26098d94500000870044ac",
        }
    ],
    "subtotal":110,
    "status":1,
    "updated_at":"2020-02-19T14:59:59.000Z",
    "created_at":"2020-02-19T14:59:59.000Z"
}

I want to Find data where order_item=>info ID .. Im using Mongo db

$db = Table::where(''Dont Know what to do)->first();

How can I do that ???


Solution

  • Have a look at the $elemMatch documentation in MongoDB

    db.orders.find({ "order_item" : { "$elemMatch" : { "info" : "5e26098d94500000870044ac" } } });

    Should do the trick.