WHAT'S NEW?
Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts
We recently had a requirement where were required to update an array inside the document. Array is a filed inside my document apart from few simple datatypes.

Here is the current document structure




I want to add a JSON to this ARRAY field as shown below.

Once added, the document looks like this




This is how we do it in the code. There are many ways apart from this yet, I found this more readable and more easy to use.

Step by step installation of Nodejs application to Heroku Cloud platform for beginner

Hi friend, this time I have decided to write on how to deploy Nodejs application to Heroku cloud platform. This is such an important article on my blog and in my faith of sharing knowledge helps us more. This is because I have had a very tough time understanding on how Heroku works and how to push an application(Nodejs) in my case. I spent almost 2-3 days of sleepless nights apart from my working hours at office, and 3 hours of travel everyday. 
So here we go...
Points to note, I did not address on how to create an account in Heroku or what is Heroku dashboard etc as they are basic steps and are easy to do. I have used Mongodb as my backend and I use a separate Mongo lab account and call it in my app than getting it registered inside the Heroku as an add on. The add on via Heroku is a chargeable account.


Today while working on MongoDb, i had a power interruption and the system has restarted. when i tried to start mongod.exe with the command

>mongod.exe --dbpath E:\MyFolder\bin

it threw me an error stating there was an issue with the close connections of Mongod. This means that data sync has some issues and so mongod cannot start smoothly
The solution for this is:

check if there is a file named mongod.lock in the bin folder. If yes, check if its size is greater than 0kB.
If yes, then delete this file, because this is the one that is intimating mongod that there was an abrupt closure of the database the previous time.
now go back to the command line and say

>mongod.exe --journal (in case journal is not set for your db)
>mongod.exe --repair

this should set the database back in line and up and running.
if you get another error like
initandlisten] exception in initAndListen: 10296 dbpath (/data/db) does not exist, terminating

then exit the cmd line and rerun the repair command and then say

>mongod.exe --dbpath E:\MyFolder\bin

to set everything back in line.
Using postman app for chrome, i was trying to post to my node js application listening to port 3001.
however, all the body of the data is sent as empty.
Because of this, the only data that is getting saved in mongodb using the mongoose connection was as below:

{
    "_id": "542e4ea1480f9abc13239976",
    "__v": 0
}

The issue with the code was that the body parser was placed at the end after the mongo connection was created and just above the app.listen(3001) line

When i changed the code to the top of the file just after the line

var app = express();
it worked for me.

now my complete code looks like this


var express = require('express');
mongoose = require('mongoose');
fs = require('fs');

var app = express();

app.configure(function () {
    app.use(express.logger('dev'));     /* 'default', 'short', 'tiny', 'dev' */
    app.use(express.bodyParser());
});


var mongoUri =  'mongodb://localhost/MyTestDatabase';
mongoose.connect(mongoUri);

var db = mongoose.connection;

db.on('error',function(){
throw new Error('unable to connect to db');
});



require('./models/MyTestModle');
require('./routes')(app);

//moved from here to the yellow highlighted section

app.listen(3002);
console.log('Listening on port 3002...');