【发布时间】:2021-11-12 04:23:48
【问题描述】:
我正在尝试删除在第一个架构中引用的第二个架构
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,
location : String,
startDate : 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}); // I can't delete my shop data
let deletedTask = await Owner.deleteOne({ ownerId: id });
} // I can delete owner but not shop detail
};
但它不会删除我的相关架构
【问题讨论】:
-
好吧,
Owner架构没有ownerId字段。您正在尝试deleteOne({ ownerId: id }),必然没有任何文档匹配。 -
实际上
ownerSchema有它自己的 ID,它的_id是默认出现的,我在这里也添加了我的ownerId@JeremyThille -
shopPlace来自哪里? -
是的,你是在我第一次发表评论后添加的 :)
-
但在你的例子中是
undefined
标签: javascript node.js mongodb mongoose mongodb-query