猫鼬不会从JSON数组创建子文档 [英] Mongoose doesn't create subdocument from JSON array

查看:114
本文介绍了猫鼬不会从JSON数组创建子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将包含第一层数据和数组的JSON对象写入MongoDB.

I'm trying to write a JSON object that contains both first-level data along with arrays into MongoDB.

发生的是,所有的第一级数据都被存储了,但是数组中包含的所有数据都没有存储.记录服务器接收的数据时,我看到了整个对象,这使我相信Mongoose代码有问题.

What happens instead is all first-level data is stored, but anything contained in an array isn't. When logging the data the server receives, I see the entire object, which leads me to believe there's something wrong with my Mongoose code.

例如,如果我发送如下内容:

So for example if I send something like this:

issueId: "test1",
issueTitle: "testtest",
rows: [
  {order:1,data: [object]},
  {order:2,data: [object]},
]

仅存储以下内容:

issueId: "test1",
issueTitle: "testtest",
lastUpdated: Date,

我为Mongo使用以下模型:

I have the following model for Mongo:

//model.js
var mongoose = require('mongoose');

var model = mongoose.Schema({

  issueId     : String,
  issueTitle  : String,
  lastUpdated : {type: Date, default : Date.now},

  rows  : [{
    order   : Number,
    data      : [
      {
        title   : String,
        text    : String,
        link    : String,
      }
    ]
  }]

});

module.exports = mongoose.model('Model', model);

还有我认为可能存在问题的路由代码:

And the routing code, where I believe the problem likely is:

//routes.js
const mongoose = require('mongoose');
const Model = require('./model.js');
...
app.post('/api/data/update', function(req, res) {
  let theData = req.body.dataToInsert;

  console.log(JSON.stringify(theData,null,4));

  Model.findOneAndUpdate(
    {issueId : theData.issueId},
    {theData},
    {upsert: true},
    function(err,doc){
      if(err) throw err;
      console.log(doc);
  });
});

同样,这是Angular控制器中存储数据的部分.我认为这里没有任何问题.

As well, here's the part of the Angular controller storing the data. I don't think there's any problem here.

pushToServer = function() {
    $http.post('/api/data/update',{
        dataToInsert : $scope.dataObject,
    }).then(function successCallback(res){
        console.log("all good", JSON.stringify(res,null,3));
    }, function errorCallback(res){
        console.log("arg" + res);
    });
  }

推荐答案

查看猫鼬常见问题解答中的第一个问题: http://mongoosejs.com/docs/faq.html

Look at the first question in the mongoose FAQ: http://mongoosejs.com/docs/faq.html

猫鼬不会为数组索引创建getter/setter.没有它们,猫鼬永远不会收到有关更改的通知,因此也不知道要坚持新的价值.解决方法是使用Mongoose> = 3.2.0中可用的MongooseArray#set.

Mongoose doesn't create getters/setters for array indexes; without them mongoose never gets notified of the change and so doesn't know to persist the new value. The work-around is to use MongooseArray#set available in Mongoose >= 3.2.0.

// query the document you want to update
// set the individual indexes you want to update
// save the document
doc.array.set(3, 'changed');
doc.save();

编辑

我认为这将更新所有行.我想知道它是否有效.

I think this would work to update all of the rows. I'd be interested to know if it does work.

let rowQueries = [];
theData.rows.forEach(row => {
    let query = Model.findOneAndUpdate({
        issueId: theData.issueId,
        'row._id': row._id
    }, {
        $set: {
            'row.$': row
        }
    });
    rowQueries.push(query.exec());
});

Promise.all(rowQueries).then(updatedDocs => {
    // updated
});

这篇关于猫鼬不会从JSON数组创建子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆