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 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;
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 get all error messages. Why is this happening? Please help me with this.
You have been enable authentication for your database.
So, you have to provide the corresponding credentials in your connection string
Change:
mongoose.connect('mongodb://localhost/mApp');
To
mongoose.connect('mongodb://username:password#host:port/database');
More information on mongoose documentation
Related
I am trying to save record into mongodb using node.js and for that purpose I am using mongoose driver but here I am unable to insert record into mongodb. I am explaining my code below.
mongo.util.js:
const mongoose = require('mongoose').Mongoose;
const config = require('../../config/settings');
const mongooseInstance = new mongoose();
const url = `mongodb://${config.MONGO_USER}:${config.MONGO_PWD}#${config.MONGO_URL}/${config.MONGO_DB}`;
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
};
/*
1- Connect to mongo server
*/
mongooseInstance.connect(url, options, (err) => {
if(!err) {
console.log('Mongodb connection successed');
} else {
console.log('Error in DB connection:' + JSON.stringify(err, undefined, true));
}
})
module.exports = mongooseInstance;
This file is my connection file where I can connect to my local mongodb. This file has included into mt app.js file and I am getting the message as Mongodb connection successed.
users.model.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const User = new Schema({
name: {type: String},
mobile: { type: String},
password: { type: String},
email: { type: String},
city: { type: String}
}, {
timestamps: {
CreatedAt: 'CreatedAt',
UpdatedAt: 'UpdatedAt'
}
});
module.exports = mongoose.model('customers', User);
The above file is my schema file where I am trying to design schema for customer collection.
users.service.js:
const _ = require('lodash'),
axios = require('axios'),
model = require('../model/users.model');
async registerUser(req) {
try{
const data = req.body;
console.log('data', data);
const user = await model.create(data);
if (!user) {
return {
data: user,
error: true,
msg: 'User Registeration failed'
}
}else {
return {
data: user,
error: false,
msg: 'User Registered successfully'
}
}
}catch(error) {
console.log('Error in registerUser service::', error);
}
}
Here I trying to insert the record but when this function is called no record is inserting into mongodb even no customer collection is there. Here I need to insert record using this mongoose driver.
Try as below
Step 1: create object out of User model
var user = new model(req.body)
Step 2: then call
user.save(function(){})
When registering, my user doesn't get saved to the database. Does anyone know what is going wrong here?
Schema + model:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/testusers', {useNewUrlParser: true});
const Schema = mongoose.Schema;
let userSchema = new Schema({
steamId: String,
username: String,
avatar: String,
tradelink: String,
balance: Number,
});
let userModel = mongoose.model('User', userSchema);
Find and save:
userModel.findOne({ steamId: profile.id } , (err,user) => {
if(!user) {
let newUser = new userModel({steamId:profile._json.steamid, username: profile._json.personaname,avatar: profile._json.avatar, tradelink: '', balance:0});
console.log(newUser);
newUser.save((err,user) =>{
console.log(err);
console.log('created user');
return done(err,user);
});
}
else {
console.log('user exists');
return done(err,user);
}
});
Collection data (empty after save): https://prnt.sc/rmlsc2
Console output:
PS C:****> node app.js
(node:26628) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
{
_id: 5e7b78aae3903a680486cb13,
steamId: '76561198126366365',
username: 'BaeWolfy',
avatar: 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d36a7d04988b8730a0a75516a7dbfa24ee1a45fc.jpg',
tradelink: '',
balance: 0
}
null
created user
Made a stupid mistake in the connection string I used localhost instead of my atlas cluster connection string. It is working now.
I am having an issue whereas any data that exists in my MongoDB instance is being removed when I restart my Node/Koa.app. This application uses Mongoose to connect to the local Mongo instance.
Here is my code:
app.js (I have code in there to output connection to the logger)
import Koa from 'koa';
import path from 'path';
import bodyParser from 'koa-bodyparser';
import serve from 'koa-static';
import mongoose from 'mongoose';
import Config from '../Config.js';
global.appRoot = path.resolve(__dirname);
const app = new Koa();
mongoose.connect(Config.mongo.url);
mongoose.connection.on('connected', (response) => {
console.log('Connected to mongo server.');
//trying to get collection names
let names = mongoose.connection.db.listCollections().toArray(function(err, names) {
if (err) {
console.log(err);
}
else {
names.forEach(function(e,i,a) {
mongoose.connection.db.dropCollection(e.name);
console.log("--->>", e.name);
});
}
});
});
mongoose.connection.on('error', (err) => {
console.log(err);
});
The MongoDB config url being referenced in the above module is:
mongo: {
url: 'mongodb://localhost:27017/degould_login'
}
and my Mongoose model:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
let UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
lowercase: true
},
password: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
groupForUsers: [{ type: Schema.Types.ObjectId, ref: 'userGroups' }]
});
export default mongoose.model('users', UserSchema, 'users');
And one of the functions that inserts Data
async register(ctx) {
return new Promise((resolve, reject) => {
const error = this.checkRequiredVariablesEmpty(ctx, [ 'password', 'email' ]);
if(error.length) {
reject(new this.ApiResponse({
success: false,
extras: {
msg: this.ApiMessages.REQUIRED_REGISTRAION_DETAILS_NOT_SET,
missingFields: error
}}
));
}
this.userModel.findOne({ email: ctx.request.body.email }, (err, user) => {
if(err) {
reject(new this.ApiResponse({ success: false, extras: { msg: this.ApiMessages.DB_ERROR }}));
}
if(!user) {
let newUser = new this.userModel();
newUser.email = ctx.request.body.email;
newUser.username = ctx.request.body.username;
newUser.password = ctx.request.body.password;
newUser.save()
.then((err, insertedRecord) => {
When I start the app and populate data into the MongoDB using the register function I can see the data saves into the MongoDB instance correctly.
However, when restarting the application all of these records get removed Is there anything that is causing this in my code? It's impossible for me to have to keep inputting data on every app restart during development.
Your issue is with this line:
mongoose.connection.db.dropCollection(e.name);
...where your collections are being dropped on mongoose 'connected' event.
Why mongoose crashes the expressjs site?
Below is my code:
var express = require('express');
var mongoose = require('mongoose');
var app = express();
// Connect to mongodb
mongoose.connect("mongodb://localhost/testdb", function(err) {
if (err) throw err;
console.log("Successfully connected to mongodb");
// Start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
// With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our users.
var userSchema = mongoose.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 next step is compiling our schema into a Model.
var User = mongoose.model('User', userSchema);
// Set route.
app.get("/", function(req, res) {
// We can access all of the user documents through our User model.
User.find(function (err, users) {
if (err) return console.error(err);
console.log(users);
})
});
I get this on browser:
This webpage is not available
But in my terminal I get the result:
Successfully connected to mongodb
Listening on port 3000
[ { _id: 57682f69feaf405c51fdf144,
username: 'testuser1',
email: 'testuser1#testdomain.com' },
{ _id: 57683009feaf405c51fdf145,
username: 'testuser2',
email: 'testuser2#testdomain.com' },
{ _id: 57683009feaf405c51fdf146,
username: 'testuser3',
email: 'testuser3#testdomain.com' }]
Any ideas what I have missed?
The problem is that you are not writing anything in the response object in your request handler. Therefore the browser keeps waiting for the request to finish and ends up with a timeout. In your app.get(), you can update the response like this:
// Set route.
app.get("/", function(req, res) {
// We can access all of the user documents through our User model.
User.find(function (err, users) {
if (err) {
console.error(err);
// some simple error handling, maybe form a proper error object for response.
res.status(500).json(err);
}
console.log(users);
res.status(200).json(users); // setting the object as json response
//OR
// res.end(); if you don't want to send anything to the client
})
});
or something similar.
Refer the Express documentation for more details:http://expressjs.com/en/api.html#res
In the below user schema there is a foobar.events field, that I am trying to push new hashes (that are received from an API POST request) to.
var userSchema = mongoose.Schema({
foobar: {
id : String,
token : String,
email : String,
name : String,
events : [{
action : String,
timestamp : Date,
user_xid : String,
type : {type: String},
event_xid : String
}]
}
});
And here is the logic for that Express route:
app.post('/foobar/post', function(req, res) {
var jb_user_xid = req.body['events'][0]['user_xid'];
var jb_timestamp = req.body['events'][0]['timestamp'];
var jb_action = req.body['events'][0]['action'];
var jb_type = req.body['events'][0]['type'];
var jb_event_xid = req.body['events'][0]['event_xid'];
User.findOne({'foobar.id':jb_user_xid}, function(err, user) {
console.log(user);
user.foobar.events.push({
user_xid: jb_user_xid,
timestamp: jb_timestamp,
action: jb_action,
type: jb_type,
event_xid: jb_event_xid
});
user.save(function(err) {
if (err){
console.log("Error on save: " + err);
}
else {
console.log("Save successful");
}
});
});
res.writeHead(200);
res.end();
return;
});
The find method is executed successfully, but the following error is thrown when trying to save to the database: Error on save: TypeError: Object.keys called on non-object - any idea why this error is being thrown?
This thread had a similar problem, but changing the findOne to findById broke my user query.
As a side note, this is what is returned in req.body from the API:
{ events:
[ { action: 'updation',
timestamp: 1408846680,
user_xid: 'aguxwNqb_Xg87buMyP6Wiw',
type: 'move',
event_xid: 'vhAkgg1XwQvLynAkkCc8Iw' } ],
notification_timestamp: 1408846680 }
And here is what's returned from the User.findOne method
{ __v: 17,
_id: 53f7d23e432de20200970c10,
foobar:
{ id: 'aguxwNqb_Xg87buMyP6Wiw',
name: 'Test User',
token: 'W3AjaI7_iOWilcKRpmxenQWi',
events: [] }
}
This error was actually due to old data within my Mongo database. The events field was full of extra strings. I deleted these and my original code began working successfully. No changes to the above code were necessary.
I tried your code and it works perfectly.. for me.. try to check mongoose module version or something.if u have still problem please do it using update function rather than save..It would be more performance oriented.
this is the following i used
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// yay!
});
var userSchema = mongoose.Schema({
foobar: {
id : String,
name : String,
events : [{
action : String,
timestamp : Date,
user_xid : String,
type : {type: String},
event_xid : String
}]
}
});
var User = mongoose.model('user', userSchema);
/*
//used for saving.
var person = new User({ foobar:
{ id: 'aguxwNqb_Xg87buMyP6Wiw',
name: 'Test User',
token: 'W3AjaI7_iOWilcKRpmxenQWi',
events: [] }
});
person.save(function(err,data){
console.log(err);
console.log(data);
})
*/
User.findOne({'foobar.id':'aguxwNqb_Xg87buMyP6Wiw'}, function(err, user) {
console.log(user);
user.foobar.events.push({ action: 'updation',
timestamp : 1408846680,
user_xid: 'aguxwNqb_Xg87buMyP6Wiw',
type: 'move',
event_xid: 'vhAkgg1XwQvLynAkkCc8Iw' });
user.save(function(err) {
if (err){
console.log("Error on save: " + err);
}
else {
console.log("Save successful");
}
});
});