1

I am new to node.js and mongo, so sorry in advance if the answer for my question is obvious.

I am trying to get all documents in a specific collection, using Mongoose. Currently there is only one document, there will be more as soon as I'll make it work.

Here is the schema:

//modules/charts.js
var mongoose = require('mongoose');

// define the schema for our chart model
var chartSchema = mongoose.Schema({
     chart: {
        type1: String
    },
    title: {
        text: String
    },
    xAxis: {
        categories: []
    },
    yAxis: {
        min: String,
        title: {
            text: String
        }
    },
    legend: {
        enabled: Boolean
    },
    credits: {
        enabled: Boolean
    },
    tooltip: {
        shared: Boolean
    },
    series: [] 

});

// create the model for charts and expose it to our app
module.exports = mongoose.model('Chart', chartSchema);

So far so good. Insertion to MongoDB works. Retrieving the data from Mongo is more complicated.

Here is how I try to extract a single document from 'charts' collection:

 //app.js
 var Chart = require('../modules/charts.js');
 app.get('/index',  function(req, res) {

    Chart.findOne({ _id: "597448fe4cd05307209accc7"}, function (err, docs) {  

        console.log(docs);
        res.render('./pages/index',{ chart: docs });

Here is the output, this is what I see when calling console.log(docs):

{ _id: 597448fe4cd05307209accc7,
  __v: 0,
  series: [ { name: 'Salary', data: [Array] } ],
  tooltip: { shared: true },
  credits: { enabled: false },
  legend: { enabled: false },
  yAxis: { min: '0', title: { text: 'Does it work already?' } },
  xAxis: { categories: [ '2010', '2013', '2017' ] },
  title: { text: 'tratata' },
  chart: { type1: 'column' } }

As you can see, I get data: [Array] instead of the array itself. If I query mongo from console, I can see the data in that array, so it is not empty, Mongoose just doesn't return it for some reason.

And if I use find() instead of findOne() (that's what I am planning to do in the end), the output is even more frustrating:

[ { _id: 597448fe4cd05307209accc7,
  __v: 0,
  series: [ [Object] ],
  tooltip: { shared: true },
  credits: { enabled: false },
  legend: { enabled: false },
  yAxis: { min: '0', title: [Object] },
  xAxis: { categories: [Array] },
  title: { text: 'tratata' },
  chart: { type1: 'column' } } ]

The series array becomes [ [Object] ], yAxis,title becomes [Object] as well, and xAxis.categories becomes an [Array].

How can I make Mongoose return the full document or documents?

It looks like I've missed something. Spent hours trying to figure out the answer and didn't find it yet. Any help will be much appreciated.

  • 1
    Hm... I think you get that output when your result is deeply nested. With `find`, try doing `console.log(docs[0].series)`. – Mikey Jul 23 '17 at 16:29
  • 1
    Thanks, looks like it is the right direction! – Marina Mironova Jul 23 '17 at 16:48
  • More to the point is `console.log(JSON.stringify(docs,undefined,2))` or similar will display the "deep" representation of the `Object` which is not shown by a pure `console.log(docs)`. – Neil Lunn Jul 24 '17 at 03:22

0 Answers0