M001 Query Syntax

According to the lecture and Atlas Sandbox set up in the Chapter 2 M001 course, the student is led to believe that the “key”: “value” is always enclosed in “”.

In my playing with the queries, I have found that if your field is a string, then yes, you need the " " after the colon. (ex. “state” : “NY”). However, if your field is a number or integer, the " " isn’t correct. These values need to be queried without the " ". (Ex. “year” : 1989). This is not really gone over in the lessons and in future versions, it would be nice to add a section on syntax differences according to the datatype being queried.

The reason I bring this up is that in later courses, it is also not identified that " " is not needed for the integer values.

If I am wrong, then please explain why when I am doing the practice sessions on chapter 2 IDE, I cannot get a return value in the query string using
<deleted, reason: potential answer>

When I enter this it returns no documents. But when I enter <deleted, reason: potential answer> I get a printout of documents?

Thoughts?

Hello @David_Thompson,

“key”: “value”

In general, it is a good idea to enclose the key in quotes - but, it is not mandatory. The value - the quotes are required for fields of type string. In case the value is a number there should not be any kind of quotes.

In MongoDB document, there can be different types of fields - string, number, date, timestamp, array and object, are some of the types. In all these, only string type field values require the quotes. The number data types are various - integer, long, decimal and double. By default, the number is of type double.

See BSON Types.

For example, in the mongo shell I created a document in collection called as persons:

db.persons.insertOne( { name: "John Doe", age: 24 } )

Then query the documents in the collection:

db.persons.find()
{ "_id" : ObjectId("604ef5e58f4a76e3865021ab"), "name" : "John Doe", "age" : 24 }

And, check if the field value is of a particular type, using the $type operator. For example, with the following two queries, only the first one finds the match and returns the document (and the second query doesn’t return the document).

db.persons.find( { age: { $type: "double" } } )
db.persons.find( { age: { $type: "string" } } )
5 Likes

Thank you… however, my intent was to let the staff at Mongo University know that there is a potential confusion point in the course ware… In the lecture on chapter two, they are very specific as to syntax. I am unsure whether this is to establish a JSON doc or not, but in the ATLAS lesson it is very specific on the filter that the syntax is “key”: “Value”… This is not the case however. I am trying to highlight that they may want to talk about the different ways that datatypes are queried in Mongo.

Also… There are typos in Chapter 3 IDE lab. In the document it states:
5. Update a single document in the zips collection where the zips field is equal to “12534” by setting the value of the “pop” field to 17630.
This is incorrect since there is not zips field. There is a zip field… but this will add a zips field instead of selecting it for the pop field update.

  1. Find all documents in the grades collection where the student_id is 250,
    and the class_id field is 399.
    There is not class field of 399… There is a class of 339 for student 250.
1 Like

Hi @David_Thompson,

First of all thanks for your keen observations of our lab and courses. It’s highly appreciated. :clap:

We will update the concerned team regarding the problem and let the learner not indulge in any confusion in upcoming events about the uses of " " with the different datatype under MongoDB.

And about your second query that is errors in zip fields and class_id. We will fix that error.

Please feel free to reach out if you still have any questions.
:slightly_smiling_face:

Kind Regards,
Kushagra

Have another question. In Chapter 4 CRUD operations, Lab 2 $expr I question whether the answer is actually correct.

The lab asks the following:

Which of the following statements will find all the companies that have more employees than the year in which they were founded?

The four answers are:

<deleted, reason: potential answer>

However, when I entered the explained answers into my terminal, connected to the atlas cluster, I cannot get the answer from the quiz. I get different answers from the .count(). My queries are attached below.

<deleted, reason: potential answer>

Please explain why I cannot duplicate the answer according to the detailed answer in the quiz?

Detailed answer is below:

<deleted, reason: potential answer>

Never mind… Answered it… ALWAYS check your spelling… There is no “$number_of_empployees”!!

Thanks

Hi @David_Thompson,

I am not able to get what you were trying to ask by

 why I cannot duplicate the answer according to the detailed answer in the quiz?

Kindly elaborate if it’s possible…!!

Thanks,
Kushagra