Find documents with matching the field's object value

110 views Asked by At

My mongodb table has a field which has object value like

_id : ObjectId("59ad227e191cc3a4c33ade07")
user_info : {"first_name": "Shraddha", "last_name": "Banerjee", "Zip": "90242", "City": "SantaBarbara"}

I want to find the users with Zip: 90242.

I tried doing it like:

$users = User::where('user_info.Zip','=', 90242)->get();

But this gives me empty result. How can this be achieved ?

2

There are 2 answers

0
ariawan On BEST ANSWER

it seems that your zip data is stored string and when you query it you are using integer.

$users = User::where('user_info.Zip','=', '90242')->get();

try to cast zip to string when you do the query

0
Vipin On

try this instead,

$users = User::where('user_info.Zip', '90242')->get();