Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if Mongoose DB is empty

Tags:

mongoose

I know I cant query the db with find method and checks if it returns an empty array, but is there any specific method in Mongoose to check if the DB has no objects in it?

like image 539
jviotti Avatar asked May 02 '26 05:05

jviotti


1 Answers

In the mongo shell you could just do:

 if (db.getCollectionNames().length === 0) {
     // It's empty
     ...
 }

Each language's driver should have something equivalent.

like image 130
JohnnyHK Avatar answered May 06 '26 12:05

JohnnyHK