如何使用用户名和密码连接 mongodb(mongoose)?

Posted

技术标签:

?????????????????? mongodb(mongoose)???????:How to connect mongodb(mongoose) with username and password? ??????:2018-05-08 07:06:28 ??????:

?? mongoose ???????,????? url ?? mongoose ?? mongodb

?????:

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://adminuser:123456@localhost:2017/mydb');

?????

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema(
  name: String,
  username:  type: String, required: true, unique: true ,
  password:  type: String, required: true ,
  admin: Boolean,
  location: String,
  meta: 
  age: Number,
  website: String
  ,
  created_at: Date,
  updated_at: Date
  );

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;

??????

    router.post('/signup',function(req,res)
     console.log("Inside")
     var useR= new User(
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     );
     useR.save(function(err) 
      if (err) throw err;
      console.log('User saved successfully!');
     );
    );

????????????

??????:

????? createConnection() mongodb://adminuser:123456@localhost:2017/mydb' ???????,???27017 mongodb://adminuser:123456@localhost:27017/mydb' ?????1?:

????????????on??????

mongoose.connect('mongodb://adminuser:123456@localhost:2017/mydb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () 
  console.log("h");
);

exports.test = function(req,res) 
  console.log(res)
;

????:

以上是关于如何使用用户名和密码连接 mongodb(mongoose)?的主要内容,如果未能解决你的问题,请参考以下文章

yii2 如何mongodb没有密码怎么用

MongoDB用户授权和管理

MongoDB的用户管理总结

MongoDB的用户管理总结

linux怎么修改mongodb的用户名和密码

SpringBoot整合MongoDB的连接用户名和密码问题