Chapter 2 questions

The lecture video told me to connect to sandbox using the uri format like this: --uri “mongodb+srv://:…” but a closer watch seems like the format is copied from the connection tab in sandbox page and looks like this ““mongodb+srv://sandbox.h2djq.mongodb.net/myFirstDatabase” --username m001-student” so I was wondering why that is.

Secondly, I would like to ask what is the querry for count distinct (that is in sql). In mongodb I have tried the find with count function and the distinct with length function. Is there a way to combine these two to get the count distinct like in SQL?

Thank you very much.

Hi @Dat_Dao,

First of all Welcome to the Mongodb Community Forum. We wish you happy learning. :slightly_smiling_face:

Yeah you are right the connection string to connect with mongodb cluster is copied from the connection tab of the atlas. You can refer to this for more clarification.

Secondly, to count the unique values, use “distinct()” rather than “find()”, and “length” rather than “count()”. The first argument for “distinct” is the field for which to aggregate distinct values, the second is the conditional statement that specifies which rows to select(as in SQL).

Append “length” to the end of the query to count the number of rows returned. (The “count()” function does not work on the results of a “distinct” query.)

For Ex:
db.collection.distinct("restaurant_name", {“city”:”New York”}).length;

Here is an above example which counts the distinct number of restaurant_name values for records with a city value of “New York”.

Please feel free to reach out to us if you still have any doubt.

Regards,
Kushagra