Tutorial by Examples

To connect to a mongo database from node application we require mongoose. Installing Mongoose Go to the toot of your application and install mongoose by npm install mongoose Next we connect to the database. var mongoose = require('mongoose'); //connect to the test database running on defau...
With Mongoose, everything is derived from a Schema. Lets create a schema. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var AutoSchema = new Schema({ name : String, countOf: Number, }); // defining the document structure // by default the collection...
For inserting a new document in the collection, we create a object of the schema. var Auto = require('models/auto') var autoObj = new Auto({ name: "NewName", countOf: 10 }); We save it like the following autoObj.save(function(err, insertedAuto) { if (err) return cons...
Reading Data from the collection is very easy. Getting all data of the collection. var Auto = require('models/auto') Auto.find({}, function (err, autos) { if (err) return console.error(err); // will return a json array of all the documents in the collection console.log(autos)...
For updating collections and documents we can use any of these methods: Methods update() updateOne() updateMany() replaceOne() Update() The update() method modifies one or many documents (update parameters) db.lights.update( { room: "Bedroom" }, { status: "On&quo...
Deleting documents from a collection in mongoose is done in the following manner. Auto.remove({_id:123}, function(err, result){ if (err) return console.error(err); console.log(result); // this will specify the mongo default delete result. });

Page 1 of 1