MongoDB:mongod显示我的应用未经授权

时间:2017-06-12 21:48:54

标签: node.js mongodb express mongoose

我安装了mongodb,我的mongodb和db文件夹分别是C:/ mongoDB / bin和C:/ data / db。我还设置了admin用户,如上所述 https://docs.mongodb.com/manual/tutorial/enable-authentication/

现在我想执行基本的CRUD操作,需要通过Express和Mongoose对数据库mApp进行读写操作。我正在为下面的app和schema提供代码。

代码已有详细记录,因此易于理解。

App.js

var express = require('express');
var app = express();

//Invoking user
var User = require('./schema.js');

//Creating an employee object by giving values to all properties
var User1 = new User({
  name: 'Anurag',
  username: 'Anurag2',
  password: 'abc',
  admin: false,
  location: 'somewhere',
  meta: {
    age: 25,
    website: 'abc.com'
  },
  createdAt: 'Jun 11 2017',
  updatedAt: 'Jun 11 2017'
}); //Remember to provide all records,otherwise document wont be saved.

//CRUD start. Creating a user document
User1.save(function(err, employ, num) {
  if (err) {
    console.log('error occurred');
  }
  console.log('saved ' + num + ' record');
  console.log('Details ' + employ);
});

/* To retrieve documents from database, you can retrieve all at
   once, or one at a time by find(), findById(), findOne() */

//To retrieve all documents
User.find({}, function(err, data) {
  if (err) {
    console.log('error occurred while retrieving all docs');
  }
  console.log(data);
});

User.findOne({
  username: 'Anurag2'
}, function(err, data) {
  if (err) {
    console.log('error in finding one document');
  }
  console.log(data);
});

User.update({
  location: 'someplace'
}, {
  location: 'anything'
}, function(err) {
  if (err) {
    console.log('error in updating');
  }
  console.log('updated');
});

//update one document
User.findOneAndUpdate({
  username: 'Anurag2'
}, {
  admin: true
}, function(err, data) {
  if (err) {
    console.log('error in finding and updating');
  }
  console.log('updated' + data);
});

//Delete a user document
User.remove({
  location: 'anywhere'
}, function(err) {
  if (err) {
    console.log('error occurred');
  }
  console.log('removed');
});

DB Schema(schema.js)

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mApp'); //myApp is the database being connected here.


//now we open a connection
var db = mongoose.connection;
db.once('open', function() {
  console.log('Connected to Database');
});
db.on('error', console.error.bind(console, 'connection error'));

//initialising a schema
var Schema = mongoose.Schema;

mongoose.Promise = require('bluebird'); //used as mpromise was showing deprecated on console.


//creating 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
  },
  createdAt: Date,
  updatedAt: Date
});

//creating a model that uses this schema
var User = mongoose.model('User', userSchema);

//now we export this model
module.exports = User;

现在,我通过管理员登录mongo并将数据库更改为mApp。我通过节点运行应用程序。

mongod控制台显示我无权对该应用程序执行任何操作。

没有执行任何查询,我收到所有错误消息。为什么会这样?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您已为数据库启用身份验证。

因此,您必须在连接字符串中提供相应的凭据

变化:

mongoose.connect('mongodb://localhost/mApp'); 

mongoose.connect('mongodb://username:password@host:port/database');

More information on mongoose documentation