In addition, don’t forget that there are additional questions and answers on Piazza.
MongoDB Compass is telling me that it can’t connect to localhost, even though it did so in the past. What can I do?
Try restarting your laptop. If you are running the MongoDB server as a service in the background, it can sometimes stop running. Restarting your laptop should restart the MongoDB server and allow Compass to connect to it. On MacOS, you may also need to explicitly restart the MongoDB server by entering the following command from the Terminal:
brew services start mongodb-community@8.0
If you have an older version of macOS, it may also help to uninstall version 8 and download an older version of MongoDB:
brew uninstall mongodb-community brew install mongodb/brew/mongodb-community@6.0
Then, once you have installed the older version, you should start it using the following command from the Terminal:
mongod --dbpath ~/data/db
In the results of our queries, do we need to worry about the order of the documents or the order of the fields within a given document?
The order of the fields doesn’t matter. The order of the documents only matters if the problem explicitly specifies that you should sort the results.
My query looks correct, but when I enter it in MongoDB Compass, I’m not getting any results or I’m getting incorrect results. Any ideas of what the issue is?
Here are some things that are worth checking:
Make sure that you are using the imdb
database and not
the default one called test
. In MongoDB Compass, the
prompt should look like this:
imdb>
See our instructions for more details.
Make sure that you are starting with the correct collection
of documents. For example, if your query is supposed to
be accessing documents from the collection called people
,
make sure that your query begins with db.people
and not
db.movies
or db.oscars
.
Make sure that all of your field names are correct – especially
field names that you include in a selection document that you
pass into the find
method or that you use as part of a $match
pipeline stage.
When you are using the value of an existing field as the basis
a field that you are creating for your results, make sure that
you include a $
before the name of the existing field. For
example, if you wanted to create subgroups based on the year of
a movie, you would need to use a $group
stage with an _id
field that is defined as follows:
_id: "$year"
If you accidentally omit the $
, you won’t get an error,
but the results will be incorrect.
The values in my actual query results look the same as the ones in the expected results, but the Autograder is saying that the results are incorrect. Any suggestions?
Make sure that your field names are correct. For example, for
Query 5, make sure that you use a field name of num_oscars
.
For Query 3, I’m trying to apply the conditions needed to focus on years from 1990-1999, but it doesn’t appear to be working. Any suggestions?
Don’t forget that when forming a selection document that uses an implicit logical AND, you can’t have two separate subconditions that both involve the same field. For example, if we wanted to find all movies with earnings ranks between 10 and 20, the following selection document would not work:
// does NOT work! { earnings_rank: { $gte: 10 }, earnings_rank: { $lte: 20 } }
This doesn’t work because a JSON document can’t have two fields with the same name.
As discussed in lecture, one way to get around this is to use an
explicit $and
operator:
{ $and: [ { earnings_rank: { $gte: 10 } }, { earnings_rank: { $lte: 20 } } ] }
Since the earnings_rank
fields now belong to two separate
subdocuments, they don’t violate the rule that you can’t have two
fields with the same name.
Another option is to group the two inequality operators together using an implicit logical AND as follows:
{ earnings_rank: { $gte: 10, $lte: 20 } }
For Query 6, the Autograder is telling me that countDocuments
is not a function. Any suggestions?
countDocuments
is a function name that is only available on
newer versions of MongoDB. In the version that is available on the
Autograder, the function is called simply count
.
Last updated on April 25, 2025.