简体   繁体   English

使用mongoose,nodejs将文档插入mongodb数组

[英]insert document into mongodb array using mongoose, nodejs

I want to insert a record into an mongoDB. 我想在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. 我想将新文档插入values数组。

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. 我不确定将文档插入数组,并在Google上搜索但无法通过后尝试以上操作。

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) 现在已经成功创建了记录,但是新记录被创建为文档,但不在values数组下。请查看屏幕快照(下面给出的链接)

I want to create new record in values so that count after creation will be 2696 instead it created new separate document. 我想在值中创建新记录,以便创建后的计数为2696,而不是创建新的单独文档。 screenshot 屏幕截图

On your schema, values is an array, but when you do: 在您的架构上, values是一个数组,但是在执行以下操作时:

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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 mongoose 和 NodeJs 在 Mongodb 的数组字段中插入数据 - How to insert data in array field in Mongodb using mongoose and NodeJs 如何在nodejs中使用mongoose将对象数组和嵌套对象数组插入mongodb - how to insert array of objects and array of nested objects into mongodb using mongoose in nodejs 使用nodejs在mongodb中插入嵌入式文档 - insert embedded document in mongodb using nodejs 使用 mongoose 和 nodejs 在 mongodb 中保存 model 的数组 - Save an array of a model in mongodb using mongoose and nodejs 如何从Mongodb到Mongoose的嵌套文档中获取Nodejs中的数组? - How to get an Array in Nodejs from a nested document in Mongodb through Mongoose? 如何使用body-parser使用nodejs和mongoose将嵌套对象数组插入/添加到mongodb数据库中 - How to insert/add array of nested objects into mongodb database with nodejs and mongoose using body-parser 如何使用Mongoose在MongoDB中插入JSON数组 - How to insert an array of JSON in MongoDB using Mongoose 如何在nodejs中使用mongoose在没有任何文档的情况下在mongodb中创建集合 - how to create the collection in mongodb without any document using mongoose in nodejs 尝试在 Nodejs 上使用 mongoose 在 MongoDB 中保存嵌套文档 - Trying to save nested document in MongoDB using mongoose on Nodejs 使用nodejs在mongodb中更新文档中的数组字段 - update an array field in the document in mongodb using nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM