【问题标题】:Saving multiple field array in mongodb using mongoose使用 mongoose 在 mongodb 中保存多个字段数组
【发布时间】:2023-04-10 12:42:01
【问题描述】:

//这里是模型

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

// Task schema
var taskSchema = mongoose.Schema({

 tasktype  : {type: String},
 createdon : {type: Date, default: Date.now},
 createdby : {type: Schema.Types.ObjectId,ref: 'User'},
 visitedby : [{type: Schema.Types.ObjectId,ref: 'User'}],
 taskinfo  : [{ isactive:Boolean, taskobject:String, taskdetails:String, iscompleted:Boolean}]  

});
module.exports = mongoose.model('Task', taskSchema);

// 路线

var Task     = require ('../models/task');
var User       = require ('../models/user');
var config     = require ('../../config');
module.exports = function(app, express) {

    var api = express.Router();

  api.post('/tasks', function (req, res) {
    var task = new Task({
 // ...
 tasktype  : req.body.tasktype,
 taskinfo  : req.body.taskinfo,
      }); 

     task.save(function(err){
        if(err){
           res.send(err);
        return;
        }
       res.json({message:'Task has been created'})
      });
return api
}

虽然所有其他字段都已保存,但具有多个字段的数组始终返回空白,例如“taskinfo:[]”

post 方法是 REST API,用于将任务发布到 mongoose 数据库中,对于具有单个字段的数组,一切正常,但具有多个字段的数组未保存,请在此帮助我。

基本帮助就可以了,请教我如何保存“多字段数组”。

Mongoose doesnot always require subdocument structure and this can be achieved by the above model, please dont advice to use subdocument structure, I want to learn this.

谢谢。

【问题讨论】:

  • 看起来您在route 的第 9 行附近丢失了一些代码。请检查。
  • 我正在询问保存数组的逻辑,因为我不知道如何保存具有多个字段的数组,createdon 默认保存,我现在不关心其他字段。
  • markModified 有效吗?
  • doc.markModified("taskinfo")
  • 我对它非常陌生,我从udemy或pluralsight的所有网络讲座中学到的是每个字段类型字段名:res.body.fieldname..你能告诉我完整的逻辑吗多字段数组?

标签: arrays node.js mongodb mongoose


【解决方案1】:

我认为如果 taskinfo 有多个值,并且您想将其保存为任务文档中的嵌入文档。您应该有不同的任务信息文档。所以,你可以这样保存

var TaskInfoSchema = require("taskInfo.js").TaskInfoSchema

var taskSchema = mongoose.Schema({

 tasktype  : {type: String},
 createdon : {type: Date, default: Date.now},
 createdby : {type: Schema.Types.ObjectId,ref: 'User'},
 visitedby : [{type: Schema.Types.ObjectId,ref: 'User'}],
 taskinfo  : [TaskInfoSchema]  

});
module.exports = mongoose.model('Task', taskSchema);

现在您将拥有不同的文档作为任务信息,例如

var taskInfo = mongoose.Schema({

     isactive:{type:Boolean}, 
     taskobject:{type:String}, 
     taskdetails:{type:String}, 
     iscompleted:{type:Boolean}

    });
    var TaskInfo = mongoose.model('TaskInfo', taskSchema);
    module.exports.TaskInfo = TaskInfo
    module.exports.TaskInfoSchema = taskSchema

当你要保存任务文件时,

 Var TaskInfo = new TaskInfo({
          isactive:true, 
          taskobject:"", 
          taskdetails:"", 
          iscompleted:true
    })



var task = {};
task.tasktype = req.body.tasktype;

你可以推它

 task.taskinfo = [];
        for (var i = 0; i < req.body.taskInfo.length; i++) {
            var taskInfo = new TaskInfo(req.body.taskInfo[i]);
            task.taskinfo.push(taskInfo);
        }

然后你将保存任务文档

var taskObj = new Task(task);

    taskObj.save(function (err) {
        if (err) {
            res.send(err);
            return;
        }
        res.json({
            message: 'Task has been created'
        })

    });
});

【讨论】:

  • 感谢先生的精彩描述,但我在某处读到不需要嵌入式文档,您能告诉我如何通过创建单一模式来实现这一点,您还可以向我推荐一些书籍或课程在那里我可以了解如何使用 mongoose 在 mongodb 中插入数组字段。 stackoverflow.com/questions/15208711/…
  • 请在设计架构之前点击此链接blog.mongodb.org/post/87200945828/…
  • 我试过你的ans,但是弹出错误,有人说我必须使用push进入&保存数组。
  • 还有 Var TaskInfo = new TaskInfo({ isactive:true, taskobject:"", taskdetails:"", iscompleted:true }) 为什么我们不接受用户的输入?
猜你喜欢
  • 2016-12-11
  • 2021-07-12
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多