Search code examples
mongodbmongodb-querymongodb-compass

Finding item in subdocument with MongoDB


I am trying to search for the current item:

Image of mongoDB compass with email highlighted

I tried the following: {participants: {userEmail: 'test@gmail.com'}}

But it did not return anything. Im mongoDB compass i get the following in my query history: Image of mongoDB query history

How would i write the query to find the highlighted email address? Or the document holding that email address.

Thanks.


Solution

  • Your query expects that participants is exactly an object like:

    {userEmail: 'test@gmail.com'}
    

    What you're trying to do can be achieved with dot notation:

    { 'participants.userEmail': 'test@gmail.com'}}