Why does the same query work in shell but doesn't work in node.js driver?

I tried query movies in shell with the script below:

db.movies.find({ countries: { $in: ["Russia", "Japan", "Mexico"] }}, { title: 1 })

It works.
However, in the node.js mongodb driver, it takes extremely long time to execute and returns all fields inside movies collection.

movies.find({ countries: { $in: ["Russia", "Japan", "Mexico"] }}, { projection: { title: 1 }})

The missing piece is the projection:. I thought the shell runs the same command as node.js, since they are both javascript.

Can someone explain to me what the difference between these two, and what else are not the same?

Hi @Luke_00153,

The mongo shell is a javascript interpreter, while the node.js driver is node library that allows you to run commands in a node.js application to MongoDB.

The syntax of of cli command line tool (such has the mongo shell) tends to be different from a driver function method syntax call.
This is somewhat confusing given that both are Javascript. Nevertheless, the driver commands and syntax varies slightly to better reflect:

  1. the typical syntax expected from node.js driver and the language native semantics
  2. the internal architecture of the mongo shell and the driver are radically different, where the nodejs driver is a native javascript library, while the mongo shell interprets js but is in reality calling a specific C driver.

Hope this clarifies your questions about the differences between the mongo shell and the node.js driver.


Thank you.
It takes me quite sometime to figure out.
I was expecting the query which works in shell should work in node.js driver.
Testing the query using shell isn’t a good idea after all :frowning: