I have installed mongodb and my mongodb and db folders are C:/mongoDB/bin and C:/data/db respectively. I have also setup admin user as stated on https://docs.mongodb.com/manual/tutorial/enable-authentication/
Now i want to perform basic CRUD operations requiring both read and write on a database mApp through Express and Mongoose. I am providing code for both app and schema below. Code is well documented so that it is easy to understand.
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 occured');
}
console.log('saved ' + num + ' record');
console.log('Details ' + employ);
});
/*To retrieve documents from dtavabse,you can retieve 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 occured 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 occured');
}
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;
Now I login in mongo through admin and i changed the db to mApp. I run the app through node The mongod console shows i am not authorized to perform any actions on the app.
No query gets executed and i receive all error messages. Why is this happening? Please help me on this.
via Anurag Sharma
No comments:
Post a Comment