These are chat archives for Automattic/mongoose

11th
Jan 2018
parthi2929
@parthi2929
Jan 11 2018 04:04
Hi I am trying to append to data array, but instead of that, my code is creating new data row. Can you please check and help?
var  mongoose = require('mongoose');

var ohlcSchema = mongoose.Schema(
    {
        //_id:{type: Number, unique: true},
        data: [[Number]]
    },
    {
        collection:'ohlc-min-DB'
    }
);

var ohlcModel = mongoose.model('ohlcModel', ohlcSchema);
var o = new ohlcModel();

mongoose.connect(
    'mongodb://localhost:27017/test',
    {
        useMongoClient:true
    }
);   
var myDBConnection = mongoose.connection;



//open the DB
myDBConnection.once(
    'open',
    function()
    {
        o.data.push(
            [1321523,23,52,12,523,5234]
        );
        o.save(
            function(error){console.log('error saving: ' + error)}
        );
    }
);

//Exit automatically after a while
setTimeout(
    function()
    {
        mongoose.disconnect();
    },
    3000
);

/*
actual result:
{ "_id" : ObjectId("5a56e0be3b8c530c346b95fe"), "data" : [ [ 1321523, 23, 52, 12, 523, 5234 ] ], "__v" : 0 }
{ "_id" : ObjectId("5a56e0cd4ba0ca117c11a741"), "data" : [ [ 1321523, 23, 52, 12, 523, 5234 ] ], "__v" : 0 }
expected result:
{"data" : [ [ 1321523, 23, 52, 12, 523, 5234 ], [ 1321523, 23, 52, 12, 523, 5234 ] ] }
*/
Kev
@lineus
Jan 11 2018 14:53
@parthi2929 are you just re-running this same script twice to push another data set onto the array?
parthi2929
@parthi2929
Jan 11 2018 14:56
I have updated it with a new code which does the job, but with ugly workaround of two schemas. one for write, one for read.
var  mongoose = require('mongoose');

var ohlcSchema = mongoose.Schema(
    {
        _id:{type: Number, unique: true},
        data: [Number]
    },
    {
        collection:'ohlc-min-DB'
    }
);
var ohlcSchemaRead = mongoose.Schema(
    {
        _id:{type: Number, unique: true},
        data: [[Number]]
    },
    {
        collection:'ohlc-min-DB'
    }
);

var ohlcModel = mongoose.model('ohlcModel', ohlcSchema);
var ohlcModelRead = mongoose.model('ohlcModelRead', ohlcSchemaRead);



mongoose.connect(
    'mongodb://localhost:27017/test',
    {
        useMongoClient:true
    }
);   
var myDBConnection = mongoose.connection;



//open the DB
myDBConnection.once(
    'open',
    function()
    {
        //update the record with new data   - THIS IS WORKING 
        ohlcModel.findOneAndUpdate(
            { _id:1},
            { $push: { 'data': [1234,23,2352,123,523,23523] } },
            { upsert: true },
            function(error, result)
            { 
                if(error) console.log('Error appending: ' + error); 
                else console.log('Data Successfully appended: ' + result); //VERY FIRST TIME RETURNS NULL THOUGH DATA APPENDED WHICH IS WRONG
            }
        );

        //retrieve entire latest data - THIS IS WORKING BUT UGLY WITH ANOTHER SCHEMA
        ohlcModelRead.findOne({_id:1},        
            function(error, result)
            {
                if(error) console.log('Error retrieving: ' + error);
                else 
                {
                    console.log('Existing Data: ' + result.data); 
                    console.log('Existing Data Rows: ' + result.data.length);
                    console.log('Existing Data Columns: ' + result.data[0].length);
                }
            }
        );
    }
);

//Exit automatically after a while
setTimeout(
    function()
    {
        mongoose.disconnect();
    },
    3000
);
I should use only one schema. If I try using 2nd one only for both read and write, data is written with each element as array.
{"data" : [ [ [1321523, 23, 52], [12], [523], [5234] ], [ [1321523], [23], [52], [12], [523], [5234] ] ] }
Kev
@lineus
Jan 11 2018 15:01
I'm pretty new at this, and in order to try and help I mocked up your code using my testDB. heres a gist of what works for me if that helps.
@parthi2929 I added a comment with what the data on the DB looks like
parthi2929
@parthi2929
Jan 11 2018 15:05
@lineus great, thank you very much, will look into it.
Kev
@lineus
Jan 11 2018 15:10
@parthi2929 what's the reasoning behind storing the data sets in a subdoc of arrays of arrays?
instead of just a collection of arrays?
parthi2929
@parthi2929
Jan 11 2018 15:10
Highcharts need that format, this is a typical financial OHLC data
Kev
@lineus
Jan 11 2018 15:12
ah, that makes sense. I was just curious :)
AnGrYbEaRcOdE
@CodeME1118
Jan 11 2018 18:10
Screen Shot 2018-01-11 at 1.09.54 PM.png

Hey, A quick question. I am following a web development course, which also is covering mongoose. We are creating a yelp clone, all about campgrounds, and I am running into one issue I cannot seem to resolve. I am getting this error:

, when running my code that looks like this:

Kev
@lineus
Jan 11 2018 19:19
@CodeME1118 I'll take a look, can you go to http://gist.github.com and use the form there to add a file for each of your relevant files? definetly seeds.js, and maybe comments.js and campgrounds.js as well.
Kev
@lineus
Jan 11 2018 20:35
instead of calling Campground.save at the end, try calling data.save
@CodeME1118
AnGrYbEaRcOdE
@CodeME1118
Jan 11 2018 20:42
OMG @lineus !!! That worked!!!! Thank you so much, I have been staring at this one for days, and you were able to do that very quickly. Thank you!
Kev
@lineus
Jan 11 2018 22:21
no worries @CodeME1118 ! every chance I get to poke around in this stuff I get better at it. It is nice to be actually helpful for a change though :)
@parthi2929 I had never used create with an array before, so I wrote this thanks for exposing me to new stuff today!