我做了一個2個mongodb模式,其中一個依賴于另一個模式,我的兩個模式看起來都是這樣的
。ownerSchema.js
var ownerSchema = Schema({
ownerId : String,
fname : String,
lname : String,
shopPlace : {
type: Schema.Types.ObjectId。
ref。'Shop'。
},
shopType String。
});
var Owner = mongoose.model('Owner', ownerSchema)。
shopSchema.js
var shopSchema = Schema({
_id : String,
shopName : String,
位置:字串。String。
startDate : Date。
endDate : Date, endDate : Date.
});
var Shop = mongoose.model('Shop', shopSchema) 。
而這是我的添加函式
const addOwner = async (req, res)=> {
const { shopName, shopLocation } = req.body.shopPlace;
const shop = new Shop({
shopName,
shopLocation,
});
await shop.save()。
const { ownerId, fname, lname } = req.body;
const newOwner = new Owner({
ownerId,
fname,
lname。
shopPlace: shop._id,
});
await newOwner.save()。
};
問題是有時我不想要shopPlace的資料,它應該是空白的
。但是我只從postman發送了ownerId、fname、lname,它沒有保存在我的資料庫中,是否有任何可能的方法,如果我不想要shopPlace,它應該保存資料到我的模式中,它應該是可選的
。uj5u.com熱心網友回復:
在你的模型中你可以為shopPlace設定一個默認值,如下:
shopPlace : {
type: Schema.Types.ObjectId。
ref。'Shop'。
default: null。
}
因此,如果你在創建一個新的Owner時沒有提供shopPlace,它將是null
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/331066.html
標籤:
上一篇:獲取當前url路徑
