2 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
這是不正確的:
const child = {
? children: {
? ? ? "firstName": "Will",
? ? ? "lastName": "Smith",
? ? ? "birthday": "9/25/1968"
? }
}
這是正確的:
const child = {
? ? "firstName": "Will",
? ? "lastName": "Smith",
? ? "birthday": "9/25/1968"
}
該記錄被插入數(shù)據(jù)庫(kù)并保存,但由于我將其作為 PUT 請(qǐng)求啟動(dòng),因此在使用HTTP 200 OK. 下面是整個(gè)解決方案的正確代碼,但是請(qǐng)記住,該res.status代碼僅在這種情況下才是必需的,因?yàn)槲沂峭ㄟ^(guò) PUT 請(qǐng)求來(lái)模仿代碼的。
貓鼬型號(hào):
const mongoose = require('mongoose');
const requiredStringValidator = [
? (val) => {
? ? const testVal = val.trim();
? ? return testVal.length > 0;
? },
? // Custom error text
? 'Please supply a value for {PATH}',
];
const childrenSchema = new mongoose.Schema({
? childId: {
? ? type: mongoose.Schema.Types.ObjectId,
? },
? firstName: {
? ? type: String,
? ? required: true,
? ? validate: requiredStringValidator,
? },
? lastName: {
? ? type: String,
? ? required: true,
? ? validate: requiredStringValidator,
? },
? birthday: {
? ? type: Date,
? ? required: true,
? },
});
const parentSchema = new mongoose.Schema(
? {
? ? parentId: {
? ? ? type: mongoose.Schema.Types.ObjectId,
? ? },
? ? firstName: {
? ? ? type: String,
? ? ? required: true,
? ? ? validate: requiredStringValidator,
? ? },
? ? lastName: {
? ? ? type: String,
? ? ? required: true,
? ? ? validate: requiredStringValidator,
? ? },
? ? children: [childrenSchema],
? },
? { collection: 'parentsjustdontunderstand' },
);
const mongooseModels = {
? Parent: mongoose.model('Parent', parentSchema),
? Children: mongoose.model('Children', childrenSchema),
};
module.exports = mongooseModels;
以下是req.body:
{
? ? "firstName": "Will",
? ? "lastName": "Smith",
? ? "birthday": "9/25/1968"
}
代碼是:
const { Parent } = require('parentsModel');
const parent = await Parent.findOne({firstName: 'Willard'});
parent.children.push(req.body);
parent.save((err, doc) => {
? if (err) {
? ? res.status(500).json({
? ? ? message: 'Error finding active projects',
? ? ? error: err,
? ? });
? } else {
? ? res.status(200).json(doc);
? }
});

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以使用 mongo 查詢將子級(jí)推送到父級(jí),因?yàn)樵?中update,第一個(gè)對(duì)象是查找推送的文檔。
語(yǔ)法如下:update({query},{update},{options})。因此,您正在尋找包含firstName: 'Willard'子項(xiàng)的文檔并將其添加到其中。
這里一切正常,所有字段都存在,父級(jí)存在于集合中,所以沒(méi)有問(wèn)題。
但使用
const parent = new Parent();
parent.children.push(req.body);
parent.save();
您的父對(duì)象是空的(除非構(gòu)造函數(shù)填充所有字段,但我認(rèn)為這不是一個(gè)好主意)。
如果你嘗試這樣做:
var parent = await model.findOne({firstName: 'Willard'})
parent.children.push(req.body);
parent.save();
然后應(yīng)該可以工作。
在本例中,對(duì)象parent是從集合中檢索的,因此它包含所有必需的字段。
我將進(jìn)行編輯以更好地解釋為什么這兩個(gè)查詢不相同。
基本上,您嘗試保存的子對(duì)象的結(jié)構(gòu)不同db.collection.update。請(qǐng)注意,您創(chuàng)建的要插入到集合中的對(duì)象child只有一個(gè)名為 的屬性children。它不具有必要的屬性,例如firstName...
我將使用純 JS 來(lái)查看console.log()輸出是什么并查看差異。
你的 mongo 查詢推送一個(gè)像這樣的對(duì)象(翻譯成 js 語(yǔ)言):
var array = []
array.push(
children = {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
)
console.log(array)
但您正在以這種方式創(chuàng)建對(duì)象:
const child = {
children: {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
}
console.log(child)
現(xiàn)在你看出區(qū)別了嗎?一個(gè)對(duì)象是子對(duì)象本身,另一個(gè)對(duì)象具有屬性children 和必要的字段。
那么讓我們將這兩段代碼組合起來(lái):
const child = {
children: {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
}
const children = {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
var array = [child,children]
console.log(array)
因此,對(duì)于您的代碼,如果您使用:
parent.children.push(child.children);
parent.save();
應(yīng)該有效。children但是,最好的方法不是在里面創(chuàng)建對(duì)象const child
嘗試使用:
const child = {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
parent.children.push(child);
parent.save();
添加回答
舉報(bào)