[SERVER-77974] $all operator does not work on on $elemMatch Created: 10/Jun/23  Updated: 27/Oct/23  Resolved: 27/Oct/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Arkensor N/A Assignee: Backlog - Triage Team
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Server Triage
Operating System: ALL
Participants:

 Description   

Hello, 

I am very new to mongo, so please forgive me if I am misusing the query syntax, but I have this situation that is either a bug or somebody please tell me how to correctly do it instead. Thank you.

I am trying to find documents where any or all nested array elements must contain at least the values provided. This can normally be achieved easily with the $all operator. For example here:

[
  {
    "words": [ "Hello", "World", "!"],
    "wordArrays": [
      ["Hello", "World", "!"],
      ["The", "Sun", "Shines"]
    ]
  }
] 

The query 

{words: {$all: ["Hello", "World"]}}

 correctly matches because both are contained and 

{words: {$all: ["Hello", "I do not exist"]}} 

 does not match. So far so good.

 

If you attempt the same for the nested arrays this stops working altogether. 

{"wordArrays.0": {$all: ["Hello", "World"]}} 

The above does not match. I have found Stackoverflow questions about why on nested arrays all the operators start to fail but no one ever can explain why and they just use $elemMatch. But I do not know how I would translate my "all values must be in the array" to an $elemMatch. How I would say

1. ANY of the wordArrays must contain "Hello" and "World" -> would match
2. ALL of the wordArrays must contain "Hello" and "World" -> would not match the data

I have prepared this little playground with the data in the hopes somebody knows how to achieve this.
https://mongoplayground.net/p/WSM2OGh-5vk

I would prefer if this could be solved all within the Find() stage, but if operations like this suddenly require the aggregation pipeline I am fine with that too.

My approach for ANY match does not return any matches, and I do not understand why 

{wordArrays: {$elemMatch: {$all: ["Hello", "World"]}}} 

I read it as wordArrays: does any element match: array contains "Hello" and "World". And that should have matched the document

Thank you!



 Comments   
Comment by Edwin Zhou [ 27/Oct/23 ]

Hi arkensor@gmail.com,

Thank you for your patience and for following up that you've sent this inquiry to various channels. It appears that you received a response from the community forums, and we would like to encourage you to further pursue your investigation there.

If that investigation leads you to believe that there is a bug, then we would investigate it here in the SERVER project.

Kind regards,
Edwin

Comment by Arkensor N/A [ 06/Jul/23 ]

Dear eric.sedor@mongodb.com,

thank you for your reply. I did indeed already open both a forum post and stack overflow issue about this a month ago, but neither resulted in any suitable workarounds. I was overall a bit disappointed to not get a single response on the forums, but I assume its simply because nobody knows how to deal with it - because it just is a bug and there is nothing else one could do.

Please investigate this issue further in the correct expert developer team

Comment by Eric Sedor [ 05/Jul/23 ]

For this issue we'd like to encourage you to start by asking our community for help by posting on the MongoDB Developer Community Forums.

  • If the discussion there leads you to suspect a bug in the MongoDB server, then we'd want to investigate it as a possible bug here in the SERVER project.
  • If a good way of satisfying your use-case can't be found there, could you submit a feature request to our MongoDB Feedback Engine?

Thank you,
Eric

Generated at Thu Feb 08 06:37:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.