I'm running mongodb version 3.6.2 on osx and have been experimenting with mongodb operations such as insert, query, delete via node.js (using the official mongodb driver for node.js).
I also am running a front-end client application for mongodb (Robo3T - version 1.2.1) as I learn more about mongo, how it works and stores data.
I'm able to perform operations such as insert (one and many records), delete based on a query filter, list ALL records, etc. But my problem comes when I go to the shell and try to run the following command:
db.getCollection('videos').find({})
No records are returned and no errors. Interesting thing is, in Robo3T, when I double click the collection, that same console command is shown (and all records are displayed, as expected).
And from node.js, when I run the following command, I DO get all the records listed:
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("collection_archivedvideo");
dbo.collection("videos").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
console.log(result.length)
db.close();
});
});
What am I doing wrong in the mongodb shell when I try to list all records? I don't think it should matter, but the collection has about 1,500 records and were imported as a bulk / many import from a local JSON dataset/file.
The only thing that I can imagine is that you aren’t connected to your database when using the shell.
Did you explicitly tell the shell to use your database?
To see the database you’re currently using run:
db
To get a list of all databases:
show dbs
And to finally switch a database:
use <dbname>
Hope this helps!