Multiple match in a sub-sub documents

Hi all,
I have a collection such as this Mongo playground

I need to have a seach with this three condition:

  1. key3 are not true
  2. all key are false
  3. other key match

How I can go to operate in parameters.options?

The parameters can be more than one

What did you try so far?

Which issues did you get with what you try?

What do you mean by the following?

Since parameters and options are both arrays, start by looking at https://docs.mongodb.com/manual/reference/operator/query-array/

Hi @steevej,
if I try to search for parameters.options.id(“60548851b5a553690d392019”) the key2:true, I expect the first collection (that ave all key of this option at false) second collection (that have exactly key2 of this options at true). The third collection are not in output because, for this parameters options, has key3 at true.

Now I try with $filter but doesn’t works. I view array query link

Hi all,
I have try to $unwind the parameters and next project to have options array to root,
such us this: Mongo playground

Next, I have try to check if all key are false and in this case return true to variable with $group.
The Idea is to have 3 check that return true and, if all check are true, I can return the id of collection.

Do you can help me to solve it?
I have found the operator $allElementsTrue, but I don’t find the opposite false.
The idea is this, for step 2, this: Mongo playground

Starting with $unwind was a good start. Using a divide and conquer approach, I came up with:

[
	{
		"$unwind" : "$parameters"
	},
	{
		"$match" : {
			"parameters.id" : ObjectId("60548851b5a553690d392019")
		}
	},
	{
		"$match" : {
			"parameters.options" : {
				"$elemMatch" : {
					"value" : "key3",
					"sel" : false
				}
			}
		}
	},
	{
		"$match" : {
			"parameters.options" : {
				"$elemMatch" : {
					"value" : "key2",
					"sel" : false
				}
			}
		}
	},
	{
		"$match" : {
			"parameters.options" : {
				"$elemMatch" : {
					"value" : "key1",
					"sel" : false
				}
			}
		}
	}
]

You probably could do the parameters.options part inside a single $match. However, I prefer using multiple $match stages when things get complicated. It is easier to understand and modify.

You may optimize the pipeline by adding another

{
		"$match" : {
			"parameters.id" : ObjectId("60548851b5a553690d392019")
		}
	}

as the first stage. Especially if you have an index. This way you only apply the $unwind to documents that have the correct parameter. The second match will then take care of removing the extra parameters.