Get value out of array of objects

I have an array of customers, for each customer there is an object (which is a reference to that object) of action. For each action I have status, which is a number between 1 to 7.

I want to search all customers, find the ones which the actions array it not empty and then check if status in one of the objects equals 4 or 5.

First when using the Customer.find i’ve added populate(‘actions’) to it, so instead of the objects reference it will return the object (which he does)

Then I tried these 2 queries, but none of them return anything:

'actions': { 
  $exists: true, 
  $ne: [], 
  $elemMatch: { status: { $in: [4, 5] } }

And this:

"actions": { "$exists": true, "$ne": [] },
"actions.status": { "$in": [4, 5] }

I also tried to simplify the query:

'actions': { 
  $exists: true, 
  $ne: [], 

And it does return the right customers.

Any idea?

Adding some sample document along your description will help greatly.

If I understand you correctly then I can think of a couple of queries, however the first query worked on the data I created so I may indeed be misunderstanding.

If you’re confident of you schema then simply:

  "actions.status": {
    $in: [ 4,5 ]

If you have an object that has the field action:{status:4} then this would also be returned.

To avoid those:

It didn’t worked, this how the objects looks like:

In the example it’s working, but on the db itself, it does not return any results.
Does the fact that the Action object is connected to the customer using a reference may suggest the issue? Even when populating the .find on customers with ‘actions’?

Screenshot 2024-01-19 at 3.05.38

Yes, there would need to be a lookup:

Just matches status 4 or 5:

Hydrates the array: