猫鼬不会从JSON数组创建子文档

编程入门 行业动态 更新时间:2024-10-18 14:17:46
本文介绍了猫鼬不会从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); }); }

推荐答案

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

Look at the first question in the mongoose FAQ: mongoosejs/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数组创建子文档

本文发布于:2023-11-22 13:33:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   文档   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!