Chapter 4: Advanced CRUD Operations Lab: Array Operators and Projection questions

Lab: Array Operators and Projection

Problem:

To complete this exercise connect to your Atlas cluster using the in-browser IDE space at the end of this chapter.

How many companies in the sample_training.companies collection have offices in the city of Seattle?

Copy/paste your answer to the response field.

In this lab, I am looking for element “seattle” in “tag_list”, so my code is:

db.companies.find( { “tag_list”: { “$elemMatch”: { “tag_list”: “seattle” }}}).count()
But it returns result as 0.

I have no idea why I am wrong, could someone tell me plz? I

Hi,
i think the problem is how you’ve write seattle.

Try it:
db.companies.find( { “tag_list”: { “$elemMatch”: { “$eq”: “Seattle” }}}).count()

Regards

Thx for your advice, but still 0, I am wondering if my code is some how wrong.

Hi,
this is the general statement
{ field: { $elemMatch: { query1, query2, … } } }

so, you need to change the part of query

db.companies.find( { “tag_list”: { “$elemMatch”: { “$eq”: “Seattle” }}}).count()

Regards

Read the lab requirement correctly.

The name of the city starts with an upper case like hinted

But the most important part is have offices in the city of

Why do you look for a field named tag_list rather than a field named officies?

1 Like

My bad for looking for office location in wrong place, below is the right code:

db.companies.find({“offices”:{"$elemMatch":{“city”:“Seattle”}}}).count()

Hi @Tianyi_Hu, welcome to the community.
Please avoid posting answers and queries leading to answers directly in the thread, it may act as a spoiler for learners who are coming here for some directions and hints.
You can use spoiler tags as below to blur your queries/code leading to the correct answers to labs:

[spoiler]Insert answers and queries leading to correct answer comes here[/spoiler]

and it will be represented as:

Insert answers and queries leading to correct answer comes here

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

3 Likes