this is my code
var mongoose = require('mongoose');
db = mongoose.createConnection('mongodb://localhost/myDB');
there is collection users
in myDB, how can i get number of documents in users
?
this is my code
var mongoose = require('mongoose');
db = mongoose.createConnection('mongodb://localhost/myDB');
there is collection users
in myDB, how can i get number of documents in users
?
Have you even tried to do this yourself?
None the less,
db.open (function(error){
db.collection("users", function(error, collection){
collection.count({}, function(error, numOfDocs) {
console.log(numOfDocs + ' total users');
});
});
});
That should get you started, I ahven't tested this code but I remember doing it like that.
try this way, its a snippet from my dummy code and it should work
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myDB'); // Connect to mongodb
mongoose.connection.once('open',(db)=>{ // opens the connection for us
console.log("Connection made to mongodb");
mongoose.model('<collection>').count().then((count) => { // replace <collection> with collection name
console.log(count);
})
}).on('error',(error)=>{
console.log("connection error to mongodb : " + error);
});