node.js - Can't find Db in mongodb while using mongoose -
the following code, works fine giving output .
var mongoose=require('mongoose'); var dburl='mongodb://localhost:27017/trial1'; mongoose.connect(dburl); //creating schema var userschema=new mongoose.schema({ name:string, email:string, createdon:date }); mongoose.model('user',userschema); var user=mongoose.model('user'); var userone=new user({name:'mike'}); console.log(userone.name); mongoose.connection.on('connected',function(){ console.log('mongoose connected '+dburl); }); mongoose.connection.close(function(){ console.log('connection closed!!!!'); });
but when try search db in connection string (ie)trial1, not able find it, screen shot follows.
also when "use trial1" in mongo shell i'm getting output per following screen shot. means db exists or been created. why not able see db??
so yes answer in comment of blakes , part of answer pio.
you don't see databases because mongoose code not create user in it. when instantiate model user create in memory not in database. insert user in database must call method save on instance this:
var userone=new user({name:'mike'}); userone.save(function(err, user){ if (!err) { console.log('now user saved in database'); } })
so if don't save user, in mongo shell won't see databases empty , not exists.
Comments
Post a Comment