MongoDB CRUD Using Mongoose

Initialize Mongoose and Schema

var mongoose = require('mongoose');
mongoose.connect('mongodb://YOURVALUES.mongolab.com:11111/NAME');
var schema = new mongoose.Schema({ name: 'string', account: 'string', date: 'string' });
var accountz = mongoose.model('accountz', schema);

Create

   var small = new accountz({
        name: "SAMZ",
        account: "Monthly",
        date: "29/12/2015"
    });
    small.save(function (err) {
        if (err){
            console.log("Error in Save");
        }else{
            console.log("Save Sucessfully");
        }
    });

Read

   accountz.find().exec(function(err, data){
        if (err){
            console.log("Error in Reading");
        }else{
            console.log("The value = " + data);
        }
    });

Update

   accountz.findOne({ "_id": "0023"}, function (err, doc){
        doc.name = editObj.name;
        doc.account = editObj.account;
        doc.date = editObj.date;
        doc.save(function (err) {
            if (err){
                console.log("Error in Updating");
            }else{
                console.log("Updated Sucessfully");
            }
        });
    });

Delete

    accountz.remove({ "_id": "0023"}).exec(function(err, data){
        if (err){
            console.log("Error in Deleting");
        }else{
            console.log("Deleting Sucessfully");
        }
    });

You can refer to my github project Explore Here .

Advertisements

Published by

shiyam

My passion became my profession , I'm a driven, energetic and proactive tech professionally. I consider myself an engagement professional, as I don't simply provide solutions, I create them. I am a continuous learner – always looking for new technologies, in my spare time I search Google for the latest trends . I like to think of myself as a futurist and I'm a Full Stack JavaScript Developer. >> Skills << ✔ Web App Development ✔ Strong Javascript ✔ Backbone.js, Angular.js, Dojo.js ✔ jQuery.js ✔ Node.js ✔ MongoDb , MySql ✔ Socket.io / Web Sockets (Native) ✔ PHP ✔ Ruby (Watir - Automation) ✔ Grunt ✔ Github , Bitbucket , SVN ✔ Agile Process ✔ Scrum ✔ Ability to research and adapt new techs ✔ Fast Learner ✔ Always Punctual ✔ Critical Thinker ✔ Friendly Personality

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s