如何迭代对象数组,以便在Mongodb嵌套数组中推送数据
How to iterate through array of object in order to push data in Mongodb nested array
我正在开发一个博客应用程序,我正在编写一个在线编辑器,没什么特别的。
在我开始解释我的问题之前,考虑一下我正在使用的模式:
var blogSchema = restful.model('blog-schema', mongoose.Schema({
title: String,
preview: String,
content: [{tag: String, body: String}],
comments: [{ body: String, date: Date}],
createdAt: {type: Date, default: Date.now}
}
在我的客户端,我使用react
来POST数据,看起来像这样:
[{"type":"h2","body":"azeaeazeae"},{"type":"p","body":"azeaeazeae"}]
然后在express()
中我这样做:
blogSchema.update(
{title: "please work AGAIN"},
{
$pushAll: {
content: test
}
},
function(err, stat, docs) {
console.log(stat);
}
)
然后用POSTMAN检查数据是否保存良好,我得到了这个:
"content": [
{
"tag": "[object Object],[object Object]",
"_id": "57b2eced869e03821d446c38"
}
我的问题,我如何在我的服务器中迭代这个对象数组,然后将每个单独的项目推到各自的位置:tag
和body
。
我确实找到了:
下面是代码:app.post('/admin', function(req, res) {
var test = req.body;
test.map(function(test, i) {
blogSchema.update(
{title: "please work AGAIN"},
{
$push: {
content: {
test: test.type,
body: test.body
}
}
},
function(err,stat,dude){
console.log(stat);
})
})
我简单地映射了请求。然后在mongo中执行$push…我很抱歉发布了这个问题。
相关文章:
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- Mongodb$in以与数组中相同的顺序获取结果
- MongoDB-通过比较集合和对象的数组来返回现有字段的数组
- MongoDB数组与对象
- MongoDB嵌套对象数组后查询
- 如何在mongodb(mongose)中覆盖子文档的数组属性
- 如何获取文档数组中元素的计数-MongoDB
- 使用mongoose将数据推送到mongoDB模式中的数组中
- 如何在mongodb中根据数组内容的差异进行排序
- angular mongodb数组格式写错了
- node.js上的过程性2d数组mongoDB插入内存不足
- MongoDB findAndModify:>>>查找和更新文档数组中的对象
- 更新数组中的嵌入文档 - Mongodb + Node Driver
- MongoDB 无法将$addToSet应用于 Meteor JS 中的非数组
- Expressjs&集合中的MongoDB更新对象's数组
- 更新数组MongoDB Meteor内部的对象
- 如何将返回的值推送到数组mongodb中
- 拉子文档在数组mongodb
- 如何查询包含数组MongoDB中某个元素的特定值的单个文档
- 错误,而插入javascript数组mongodb使用猫鼬