These are chat archives for Automattic/mongoose

13th
Feb 2017
kailash k yogeshwar
@kailashyogeshwar85
Feb 13 2017 05:30
i am trying save the image uploaded from postman as base64 in mongodb so the 512kB image will have the equivalent base64 string in db everything is fine on local machine but it takes alot of time when deployed on server should be image be compressed more and then stored and how to reduce the latency due to this huge image data
Isak La Fleur Engdahl
@isaklafleur
Feb 13 2017 17:13
Hello! I have a question how to best disconnect from the database.
Im new to mongoose, btw. :)

var mongoose = require("mongoose");
var fs = require('fs');
mongoose.Promise = require('bluebird');

mongoose.connect('mongodb://localhost/cd_catalog');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
// we're connected!

// create db schema
var CdSchema = new mongoose.Schema({
TITLE: 'string',
ARTIST: 'string',
COUNTRY: 'string',
COMPANY: 'string',
PRICE: 'string',
YEAR: 'string'
});

// Create model
var Cd = mongoose.model('Cd', CdSchema);

// Remove all old records from the collections
Cd.remove({}, function() {
console.log("Removed all previous records in Collection")
});

// Read file and store data into db
fs.readFile('./json-files/cd_catalog.json', 'utf8', function (err,data) {
data = JSON.parse(data);

for(var i = 0; i < data.CATALOG.CD.length; i++) {
  var newCD = new Cd();
  newCD.TITLE = data.CATALOG.CD[i].TITLE;
  newCD.ARTIST = data.CATALOG.CD[i].ARTIST;
  newCD.COUNTRY = data.CATALOG.CD[i].COUNTRY;
  newCD.PRICE = data.CATALOG.CD[i].PRICE;
  newCD.YEAR = data.CATALOG.CD[i].YEAR;
  newCD.save(function (err) {});
}
console.log("Data saved to db");

});
});

Where should I add command mongoose.disconnect(); if I place it in the end, the data is not stored...