When no document comes out from a pipeline, any of the following could be wrong.
You have the wrong database
You have the wrong collection
You have the wrong query, $match in this case
I would proceed in the following way.
Remove the $match stage and see if I have any document. If you have no document, then is is problem 1. or 2. Check both names. Note that the name can be correct but you might be on the wrong cluster.
Add back each of $match clauses one by one verify if you have documents or not.
When you have no documents, you know that the culprit is the last clause added.
3a. Check if the field name is correct? Names are case sensitives.
3b. Check if the value checked has the same type as the fiekd values
I have checked you query directly on Atlas, and your query seems fine giving 730 total (no limit) documents in return.
I haven’t tried other parts in a Node.js environment but here I suspect two things: serverApi: ServerApiVersion.v1 and .toArray(). They might not be working to your expectation.
open a node REPL in your project folder (so you can access installed libraries in it). try without setting serverapiversion and then try to print the result of aggregation directly.
The problem is in one of your username, password, or cluster address. wait about 30 seconds and check your log to see “Uncaught MongoError: Topology is closed, please connect”
I have tried this on my cluster, the only differences are those 3, and your code works fine.
there is a possibility your driver is not installed correctly. but first, try connecting to your cluster with mongo shell or Compass to see if your URI is fine.
I have node.js v18.0.0 and npm v8.6.0. mongodb v4.9.1 gets installed, and your code run perfectly to return a result in a few seconds.
maybe nodejs installation has a problem, or your firewall happened to disallow node to use the network.
or maybe you forgot to import the sample database in that cluster which results in zero response because no document returns. this type of forgetfulness can happen anytime, so go check that too.
I manage to get “Uncaught ReferenceError: client is not defined” error message. Unsure what this is referring to. But regardless thank you for assisting
a new idea to test if your installs have a problem:
go to https://github.com/Schniz/fnm go to and download this very lightweight node version manager. it is easy to install/uninstall; single executable, a single folder, and easy commands to start. plus this makes it kinda portable manager; eval "$(fnm env)" when you need these versions of node.
set a new node version, start a repl, try above fresh project to test your code. since the node’s executable will be on a different folder path, freshly installed, and should not be broken if no network errors, then your code should work fine too. if not the culprit is your OS or PC.