I'm trying to use mongodb
with node.js
using Mongoose ODM
.
I've written some sample code which is given below:
Sample Code -
/*!
* mongoose.js
*
* Getting Started
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var db = mongoose.connect('mongodb://localhost/trymongo').connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connected to database"); // we're connected!
// create schemas
var SubjectSchema = new Schema({
name: {
type: String,
required: true
},
teacher: {
type: String
}
},
{
collection: 'subjects'
});
// create a model
var Subject = mongoose.model('Subject', SubjectSchema);
var arr = [{ name: 'Computer Programming', teacher: 'M. Swaminathan' }, { name: 'History' }];
Subject.insertMany(arr, function(err) {
if (err) throw err;
console.log('Multiple subjects created!');
// get all the subjects
Subject.find({}, function(err, subjects) {
if (err) throw err;
console.log(subjects);
});
});
});
I want to print subjects in console using console.log()
returned by mongoose model but it only prints like this -
[ [object Object], [object Object] ]
I've also tried with console.dir()
but result is same as above.
Use console.log(JSON.stringify(subjects, null, 4))
More about JSON.stringify