Guest User

Untitled

a guest
Jan 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2.  
  3. var ohlcSchema = mongoose.Schema(
  4. {
  5. _id:{type: Number, unique: true},
  6. data: [Number]
  7. },
  8. {
  9. collection:'ohlc-min-DB'
  10. }
  11. );
  12. var ohlcSchemaRead = mongoose.Schema(
  13. {
  14. _id:{type: Number, unique: true},
  15. data: [[Number]]
  16. },
  17. {
  18. collection:'ohlc-min-DB'
  19. }
  20. );
  21.  
  22. var ohlcModel = mongoose.model('ohlcModel', ohlcSchema);
  23. var ohlcModelRead = mongoose.model('ohlcModelRead', ohlcSchemaRead);
  24.  
  25.  
  26.  
  27. mongoose.connect(
  28. 'mongodb://localhost:27017/test',
  29. {
  30. useMongoClient:true
  31. }
  32. );
  33. var myDBConnection = mongoose.connection;
  34.  
  35.  
  36.  
  37. //open the DB
  38. myDBConnection.once(
  39. 'open',
  40. function()
  41. {
  42. //update the record with new data - THIS IS WORKING
  43. ohlcModel.findOneAndUpdate(
  44. { _id:1},
  45. { $push: { 'data': [1234,23,2352,123,523,23523] } },
  46. { upsert: true },
  47. function(error, result)
  48. {
  49. if(error) console.log('Error appending: ' + error);
  50. else console.log('Data Successfully appended: ' + result); //VERY FIRST TIME RETURNS NULL THOUGH DATA APPENDED WHICH IS WRONG
  51. }
  52. );
  53.  
  54. //retrieve entire latest data - THIS IS WORKING BUT UGLY WITH ANOTHER SCHEMA
  55. ohlcModelRead.findOne({_id:1},
  56. function(error, result)
  57. {
  58. if(error) console.log('Error retrieving: ' + error);
  59. else
  60. {
  61. console.log('Existing Data: ' + result.data);
  62. console.log('Existing Data Rows: ' + result.data.length);
  63. console.log('Existing Data Columns: ' + result.data[0].length);
  64. }
  65. }
  66. );
  67. }
  68. );
  69.  
  70. //Exit automatically after a while
  71. setTimeout(
  72. function()
  73. {
  74. mongoose.disconnect();
  75. },
  76. 3000
  77. );
Add Comment
Please, Sign In to add comment