How to search for a key-value pair in ALL objects within an array in the document

I am trying to return documents that have a key-value pair in any of the objects within an array in the document.

The query I came up with only looks at the 0th object in the array to check the key-value pair.
I can’t think of or find online what I need to change in the query so that it will search all the objects in the array.

I am using MongoDB Atlas.

The documents I’m searching: (simplified)

"orders": [
  {
    "OrderId": A,
    "LineItems": [
      {
        "LineItemId": "1"
      },
      {
        "LineItemId": "2"
      }
    ]
  },
  {
    "OrderId": B,
    "LineItems": [
      {
        "LineItemId": "2"
      },
      {
        "LineItemId": "1"
      }
    ]
  },
  {
    "OrderId": C,
    "LineItems": [
      {
        "LineItemId": "3"
      }
    ]
  }
]

The query I’m using:

"OrderQueryInput": {
  "LineItems": {
    "LineItemId": "1"
  }
}

This query only returns the following:

"orders": [
  {
    "OrderId": A,
    "LineItems": [
      {
        "LineItemId": "1"
      },
      {
        "LineItemId": "2"
      }
    ]
  }
]

My desired result is:

"orders": [
  {
    "OrderId": A,
    "LineItems": [
      {
        "LineItemId": "1"
      },
      {
        "LineItemId": "2"
      }
    ]
  },
  {
    "OrderId": B,
    "LineItems": [
      {
        "LineItemId": "2"
      },
      {
        "LineItemId": "1"
      }
    ]
  }
]

I would appreciate any help or push in the right direction.

Your queries works perfectly with the sample documents you provided.

Most likely your sample have been redacted. That is the problem with redaction of sample documents, what you removed from your documents make your query works. The issue is that by doing LineItems:{…}, what you really means is object equality, rather than field equality on LineItemId.

If you add just a simple field like

the order OrderId:B will not be selected.

The solution is to use the dot notation to specify field equality. The update query will look like:

{
  "LineItems.LineItemId": "1"
}

Firstly, I want to show gratitude and thank you for taking the time to help me.

I have forgotten to write that I am using GraphQL to access this data.
I will share screenshots of the GraphQL query and results to make things easier.

(Screenshot RED 1)
You mentioned that my query should show the right results. (and I really wish it did, haha)
Below are screenshots of my query and results when asking for the two products by id. Both return different orders. ("_id" changing)

(Screenshot BLUE 2)
Also below, the results of the query you suggested.

Thank you again,
Zanek

Sorry, I won’t be able to help you further. I know nothing about GraphQL. I avoid any abstraction layers. Plain and direct queryand aggregation works perfectly for me.

That’s okay,
Thank you for your time, I apologise for wasting it.

What’s this tho?

I am using next.js(React). Would I benefit from this instead of GQL?

I have got it!!!

Adding “_in” to the query after the id key.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.