Find command not working from shell

Below command is now working in shell

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db
movies
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.find({year: 2009}).pretty()
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.find({mpaaRating: “PG-13”}).pretty()
MongoDB Enterprise Cluster0-shard-0:PRIMARY>

There is no DB by name movies.Movies is a collection under video DB
Did you by any chance ran the command use movies?
Quit the current session
Connect again to video DB and run your query.It will work

Hi @Sachin_74671,

Your find queries are not returning anything because your are issuing the query on the on the wrong collection.

Let’s understand it this way. The name of your database is movies. The db variable points to this databse only. Now lets see the collections present inside this database.

show collections


As you can see in the output, the movies database has movieDetails collection inside it. You are supposed to use this collection. For example :

db.movieDetails.find({year: 2009}).pretty()

If you still have any query, feel free to get back to us. Happy Learning :slight_smile:

Thanks,
Shubham Rajan
Curriculum Support Engineer

I’m having this issue,and I think I’m working with the right collection

MongoDB Enterprise Cluster0-shard-0:PRIMARY> use video
switched to db video
MongoDB Enterprise Cluster0-shard-0:PRIMARY> show collections
movies
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.find({mpaarating : “PG-13”})
MongoDB Enterprise Cluster0-shard-0:PRIMARY>

Please check the field name
Should be like this
db.movies.find({mpaaRating : “PG-13”})

Hi @Declan_63392,

Yes, you are connected to the right cluster (Atlas class cluster) but the field name on which you are querying is not correct.

As mentioned by @Ramachandra_37567, please change it to mpaaRating.

db.movies.find({mpaaRating : "PG-13"})

Hope it helps.

If you still have any issue, please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Thanks Shubham, I copped that…eventually :slightly_smiling_face:

So why does the query not use proper JSON formatting?

I think it does. But may be I misssed something, so could you indicate where it is not?

1 Like

Hi @Tim_Rohrer ,

Please indicate where the query is not using proper JSON formatting.

The instructor for M001: MongoDB Basics course stated proper JSON includes double quotes around field name (key) for every item.

The answer here, along with my own research, indicate the field name (for queries) are only required to be surrounded by double quotes IF the field name is not a single word.

For example, in a find call, birth date would need to be in double quotes while city would not:
db.trips.find({"birth date" : "Feb 25 1969"}) works, while db.trips.find({birth date : "Feb 25 1969"}) does not.

However, both db.zips.find({ city : "RIVERSIDE" } and db.zips.find({ "city" : "RIVERSIDE" }) work meaning the query tool doesn’t strictly conform to the JSON spec according to the lecturer.

Part of the confusion also comes from the lecture on Data Explorer where the narrator says one must enter a “valid JSON query”, which would seem to mean double quotes are required around any key name. It didn’t help then when I was working on this, the query tool through Data Explorer didn’t seem to be working, so I continued onto the console where I also ran into trouble before coming here to search the forums. Because of the answer by @Shubham_Ranjan above, I tried a bunch of queries without quotes around the key, but of course the doesn’t work with birth date which was the first one I was working with.

I also realize the my question above isn’t worded as well as it could be. Technically, it appears the query tools do allow for the use of proper JSON formatting, but they don’t strictly require it.

While I believe this could be clearer in the answers above, I do appreciate the MongoDB team’s efforts to provide these tools and instructional materials. I am grateful for that.

1 Like

Yep, I really missed that part. Principally, out of habit from Java programming, where double quotes are always required, so I was always used them.