QuikProBroNa QuikProBroNa - 4 months ago 11
Node.js Question

Mongoose: New and changing attributes in the schema

I have been working with mongoose for a while and I am currently stuck with this problem:

I have a dynamic MongoDB database on which a particular collection does not have the same attributes, except the date.

Here is the example:

"date":20160121,"RNB":11.575466025255562,"LPN":0.13705583756345177,"SST":0,"DN":0


but it can have many different attributes, there's no theoretical upper limit to as to the number of attributes.

This is what I have tried so far:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var bioChemSchema= new Schema({
date: {type: Number}

}, {strict: false});


var bcm= mongoose.model('bcm', bioChemSchema);

module.exports = bcm;


How ever, the GET request to my express does not return anything.

Here's the API code:

var bCS= require('../models/bioChemSchema');

router.get('/bio', function(req, res, next) {
bCS.find({}, function(err, data){
if(err){
throw err;
}
res.json(data);
});
});


I would be grateful for any help..

Answer

Just in case someone still has this issue, it's because mongoose appends a 's' after the name of the model to the collection. Hence, it was querying for the wrong collection all along: bcms. This was difficult to find out since all of my other collections ended with numbers( mongoose, for some reason, does not append s in this case. ) and worked fine.

This can be fixed by:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var bioChemSchema= new Schema({
    date:  {type: Number}

}, { collection : 'bcm' }, {strict: false});


var bcm= mongoose.model('bcm', bioChemSchema);

module.exports = bcm;