Running the mongod process in the background, in the mongo shell (mongosh), I have the wikipedia database loaded in my database "enwiki". So in the shell, I can just type db.pages.find(), and it will show me some pages from Wikipedia. Similarly, if I call db.getCollectionNames(), it returns ['pages']. db.getCollection("pages").find() also works.
However, if I go to my nodejs script, db.collection("pages").find() has nothing in it.
const MongoClient = require("mongodb").MongoClient;
const url = 'mongodb://localhost:27017/';
const dbName = 'enwiki';
const client = new MongoClient(url, { useUnifiedTopology: true });
client.connect().then(client => {
console.log('Connected successfully to server');
const db = client.db(dbName);
const cursor = db.collection("pages").find();
cursor.forEach(doc => { console.log(doc.plaintext);});
});
Running the above code just prints 'Connected successfully to server' and stops. (It should also print the plaintext of all my Wikipedia articles in my database.)
I'm new to mongo, so I feel like I'm missing something obvious. The database was originally created using dumpster-dive. Calling db.listCollections() returns this in the node debugger:
{ _events: Object,
_eventsCount: 0,
_maxListeners: 'undefined',
parent: Db,
filter: Object,
... }
That object does not contain the pages collection that I am expecting. How do I access the pages collection?
EDIT 1/18/22 (next day): Ran it today not in the debugger and it just worked. For some reason, running db.collection("pages").find() and db.collection("pages").find().limit(1) in the node debugger returns a similarly weird object to the one above. On the other hand, running cursor.forEach(doc => { console.log(doc.plaintext);}); in the node debugger just returns:
{ [[PromiseState]]: 'pending',
[[PromiseResult]]: 'undefined' }
And ends without doing anything else, so I thought it was doing nothing. In my not fully reduced code shown here, I also had a couple of typos within the forEach promise whose errors were getting suppressed.
The above code does work and it just didn't seem like it did. In my case, I think it was a combination of error suppression that I wasn't seeing and this weird node debugger behaviour that led me to believe it wasn't working. I would love an explanation for why this debugger behaviour is happening and how to avoid/fix it though.