我試圖洗掉我的第二個模式,該模式在第一個模式中具有參考價值
ownerSchema.js
我試圖洗掉我的第二個模式。
ownerSchema.js
var ownerSchema = Schema({
ownerId : String,
fname : String,
lname : String,
shopPlace : {
type: Schema.Types.ObjectId。
ref。'Shop'。
}
});
var Owner = mongoose.model('Owner', ownerSchema)。
shopSchema.js
var shopSchema = Schema({
_id : String,
shopName : String,
位置:String: String。
startDate : Date。
endDate : Date, endDate : Date.
});
var Shop = mongoose.model('Shop', shopSchema) 。
所以我想洗掉我的第二個模式,但它只洗掉了第一個模式
。const Owner = require(" 。 ./models/ownerSchema")。)
const Shop = require("../models/shopSchema")。
const deleteOnwerById = async(req, res)=> {
const { id } = req.params;
let deletedShop = await Shop. deleteOne({_id:shopPlace}); //我不能洗掉我的店鋪資料。
let deletedTask = await Owner.deleteOne({ ownerId: id })。
} // I can delete owner but not shop detail[/span].
};
但它沒有洗掉我的相關模式
uj5u.com熱心網友回復:首先,使用findByIdAndRemove或findOneAndDelete洗掉你的所有者。這些方法允許你通過傳遞一個可選的options物件來指定一個或幾個欄位,以便從被洗掉的檔案中回傳。
let deletedOwner = await Owner. findByIdAndRemove(ownerId, {projection : "shopPlace"}) 。
然后使用所有者的shopPlace來洗掉相應的shopPlace :
await Shop. deleteOne({_id:deleteOwner.shopPlace})。
uj5u.com熱心網友回復:
你必須首先從shopPlace獲得一個你的所有者的_id:
const Owner = require(" 。 ./models/ownerSchema")。)
const Shop = require("../models/shopSchema")。
const deleteOnwerById = async(req, res)=> {
const { id } = req.params;
const owners = await Owner. findOne({ ownerId: id })。populate('shopPlace')
let deletedShop = await Shop. deleteOne({ _id:owner.shopPlace._id }) 。
let deletedTask = await Owner.deleteOne({ ownerId: id })。
}
};
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/319820.html
標籤:
