Mongoose - 无法将子文档推送到父文档中的数组中
Mongoose - Can't push a subdocument into an array in parent Document
我正在尝试将子文档 (ApplicationSchema) 推送到我的作业架构中。但是好像不行。
以下是我的工作架构:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
var ApplicationSchema = require('./Application');
const Job = new Schema({
skills : {
type : Array
},
active : {
type : Boolean,
default : false
},
applications: [ApplicationSchema],
userId : {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
},{timestamps : true});
export default mongoose.model("Job", Job)
这是子文档(ApplicationSchema)。我在这个模式中还有 5 个子文档。
我正在推送一个具有键值对 talentId 及其值的对象。但它不起作用。
我在数组中得到一个新对象,但我尝试推送的对象没有被推送。
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
var notesSchema = require('./notesSchema');
var documentSchema = require('./documentSchema');
var assessmentSchema = require('./assessmentSchema');
var interviewScheduleSchema = require('./interviewScheduleSchema');
var referenceSchema = require('./referenceSchema')
const ApplicationSchema = new Schema({
talentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Talent'
},
applicationType: {
type: Number
}
notes: [notesSchema],
documents: [documentSchema],
assessment: [assessmentSchema],
interviewSchedule: [interviewScheduleSchema],
references: [referenceSchema]
},{
timestamps: true
});
export default ApplicationSchema;
以下是我在 API 端点
中的代码
.post((req, res, next) => {
Job.findById(req.params.jobId)
.then((job) => {
if (job != null) {
job.applications.push(req.body);
job.save()
.then((job) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(job);
})
}
else {
err = new Error('Job ' + req.params.jobId + 'not found')
err.status = 404;
return next(err);
}
}, (err) => next(err))
.catch((err) => next(err));
})
req.body 包含以下对象
{ talentId: '5a813e1eb936ab308c4cae51' }
如果您已经有了作业文档的 ID,那么您可以通过执行以下操作直接推送应用程序对象:
Job.update(
{ _id: req.params.jobId },
{ $push: { applications: req.body} },
callback
);
或者您可以使用 promise 来处理这个问题。如果您只保存应用程序的 ID,那么您可能需要更改作业模式以存储应用程序的 ID 而不是整个应用程序模式。
请仔细阅读 documentation,因为这是非常基本的更新查询。
你有,
talentId: {type: mongoose.Schema.Types.ObjectId,
ref: 'Talent'}
但是您的 req.body
包含:
{ talentId: '5a813e1eb936ab308c4cae51' }
应该是:
{ talentId: mongoose.Types.ObjectId('5a813e1eb936ab308c4cae51') }
原来代码没有问题。
我使用的是导入和导出默认语法,但似乎不太适用。
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
和
export default ApplicationSchema;
我用 Common JS 语法替换了它们,一切正常。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
和
module.exports = ApplicationSchema;
我为 Job 文档文件和每个子文档文件做了这个并且代码有效。
我正在尝试将子文档 (ApplicationSchema) 推送到我的作业架构中。但是好像不行。
以下是我的工作架构:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
var ApplicationSchema = require('./Application');
const Job = new Schema({
skills : {
type : Array
},
active : {
type : Boolean,
default : false
},
applications: [ApplicationSchema],
userId : {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
},{timestamps : true});
export default mongoose.model("Job", Job)
这是子文档(ApplicationSchema)。我在这个模式中还有 5 个子文档。 我正在推送一个具有键值对 talentId 及其值的对象。但它不起作用。 我在数组中得到一个新对象,但我尝试推送的对象没有被推送。
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
var notesSchema = require('./notesSchema');
var documentSchema = require('./documentSchema');
var assessmentSchema = require('./assessmentSchema');
var interviewScheduleSchema = require('./interviewScheduleSchema');
var referenceSchema = require('./referenceSchema')
const ApplicationSchema = new Schema({
talentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Talent'
},
applicationType: {
type: Number
}
notes: [notesSchema],
documents: [documentSchema],
assessment: [assessmentSchema],
interviewSchedule: [interviewScheduleSchema],
references: [referenceSchema]
},{
timestamps: true
});
export default ApplicationSchema;
以下是我在 API 端点
中的代码.post((req, res, next) => {
Job.findById(req.params.jobId)
.then((job) => {
if (job != null) {
job.applications.push(req.body);
job.save()
.then((job) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(job);
})
}
else {
err = new Error('Job ' + req.params.jobId + 'not found')
err.status = 404;
return next(err);
}
}, (err) => next(err))
.catch((err) => next(err));
})
req.body 包含以下对象
{ talentId: '5a813e1eb936ab308c4cae51' }
如果您已经有了作业文档的 ID,那么您可以通过执行以下操作直接推送应用程序对象:
Job.update(
{ _id: req.params.jobId },
{ $push: { applications: req.body} },
callback
);
或者您可以使用 promise 来处理这个问题。如果您只保存应用程序的 ID,那么您可能需要更改作业模式以存储应用程序的 ID 而不是整个应用程序模式。
请仔细阅读 documentation,因为这是非常基本的更新查询。
你有,
talentId: {type: mongoose.Schema.Types.ObjectId,
ref: 'Talent'}
但是您的 req.body
包含:
{ talentId: '5a813e1eb936ab308c4cae51' }
应该是:
{ talentId: mongoose.Types.ObjectId('5a813e1eb936ab308c4cae51') }
原来代码没有问题。
我使用的是导入和导出默认语法,但似乎不太适用。
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
和
export default ApplicationSchema;
我用 Common JS 语法替换了它们,一切正常。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
和
module.exports = ApplicationSchema;
我为 Job 文档文件和每个子文档文件做了这个并且代码有效。