Chapter 4 examples

hello everyone,
I get an error when using this query :
db.listingsAndReviews.find({“amenities” :{"$size":{"$gt":1}}})

the question is to " Find all documents that contain more than one amenity without caring
about the order of array elements ". can anyone help please ?

We cannot help you if you do not indicate the error you are getting.

A screenshot is the best way to convey what you are doing and the issues you are having.

the screenshot is added, thank you

Your quotes are wrong around amenities.

Next time, please publish the screen shot in a new post. It is easier to follow the chronology.

still get an error…

Hi There,

I am also stuck at this question. Can you @Pawel_Kuklinski help us here. So once we navigate to the sample_airbnb database -

This is the question

Find all documents that contain more than one amenity without caring about the order of array elements. And this was not discussed in the class lecture. Thanks.

Regards,
Paul

Hi @Ouissal_KADRI and @Paul_Sujith_Rayi :wave:,
As of MongoDB 3.6, you can use “$expr” in your find queries.
Please note that “$expr” allows the use of aggregation expressions within the query language.

Since you can use “$gt” by providing an array that contains a constant value and a document to calculate the length of the array by using the “$size” operator, it will be very easy for you to filter out the appropriate documents from the collection.

I hope it helps :slight_smile:.

In case you have any doubts, please feel free to reach out to us.

Thanks. Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like