Question on Quiz 4

my thinking is mistaken & how I should frame the problem:

I am using Compass: the goal is to restrict data size and return content with an array matching some values.
as an example from Airbnb sample data: I get this from the query below: unknown operator $and

{amenities:{$and:[{$size:20},
{$all:["Pool","Microwave","WiFi"]}
]}}

Both parts individually are validstrong text :

{amenities:{$size:20}}

and

{amenities:
{$all:["Pool","Microwave"]}
}

If I pull $and to top-level as in :

{$and:[expression1, exprression2]}  it still is not valid
eg: {$and:[amenities:{{$size:20},
{$all:["Pool","Microwave"]}
]}

What have I got backward? Thanks!

You are really close to the correct query.

You are correct with

but expression1 is

and expression2 is

The major issue is that you factored amenities. The correct query is

{ "$and" : [
  { "amenities" : {$size:20} } ,
  { "amenities" : {$all:["Pool","Microwave"]} }
] }

Note that inside an array you have simple values, like strings or numbers, objects (enclosed inside {}) or arrays (enclosed inside[]).

By factoring amenities you were on the right track to use implicit and rather than explicit $and. With implicit and, that is with amenities factored, the query would look like:

{ "amenities" : {
  "$size" : 20 ,
  "$all" : ["Pool","Microwave"]
} }

You could even add a $nin such as

{ amenities: {
  $size: 20 ,
  $all: [ 'Pool', 'Microwave'] ,
  $nin: [ 'Cable TV']
} }
3 Likes

thanks for both the examples and clarity !!! You help is wonderful !

3 Likes