0

I want to insert a record into an mongoDB. DB Model is as:

var stockhistory = new Schema({
symbol:   String,
values:   [{
    date:     Date,
    open:     Number,
    close:    Number,
    high:     Number,
    low:      Number,
    adjClose:Number,
    volume:   Number
    }]
});

I want to insert new document to values array.

I have tried the following but getting error.

var insertObj = {
    'date': obj[0].lt,
    'open': obj[0].op,
    'close': obj[0].l,
    'high': obj[0].hi,
    'low': obj[0].lo,
    'adjClose': obj[0].l_fix,
     'volume': obj[0].vo
 };
var sname = 'symbol';
var History = require('../model/stockHistory');
var history = new History({ symbol: sname, values: insertObj });
history.save(function(err) {
    if (err) {
        return err;
    } else {
        console.log("Quote saved");
    }
});

And

History.findOneAndUpdate(
                                {symbol: sname},
                                {$push: {values: insertObj}},
                                {safe: true, upsert: true},
                                function(err, model) {
                                    console.log(err);
                                }

I am not sure about the document insert into array and tried above after searching on google but not getting through.

Thanks for help !

Edit

Records are successfully created now but new records are created as a document but not under values array.Please have a look at screenshot(link given below)

I want to create new record in values so that count after creation will be 2696 instead it created new separate document. screenshot

1 Answer 1

1

On your schema, values is an array, but when you do:

var history = new History({ symbol: sname, values: insertObj });

You are setting values as an object. You could try:

var insertObj = {
    'date': obj[0].lt,
    'open': obj[0].op,
    'close': obj[0].l,
    'high': obj[0].hi,
    'low': obj[0].lo,
    'adjClose': obj[0].l_fix,
     'volume': obj[0].vo
   };
var sname = 'symbol';
var History = require('../model/stockHistory');
var history = new History({ symbol: sname, values: insertArr });
History.findOneAndUpdate(
   {symbol: sname},
   {$push: {values: insertObj}},
   {safe: true, upsert: true},
   function(err, model) {
    console.log(err);
  }
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the reply...I tried the solution that you provided but record did not get inserted.
Record created now with mine as well as your solution.Error was because of typecasting of DATE field.But the problem is that new records are getting created as a seperate document but not in array.I am updating my Q with screenshot.Please have a look there.
When does this happen? this will happen when you use the save method, as you are instantiating a new history. If you just want to insert the object to an existing document then you just run the findOneAndUpdate with upsert true
second option using findOneAndUpdate has worked for me now. Thanks for the help.
Nice! I'll add this to my answers, can you please accept it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.