如何从MongoDB模型中删除关系
是否有任何可能的方法列出Mongodb中模型之间的关系并删除或编辑它们 更新架构: 公司: 产品:如何从MongoDB模型中删除关系,mongodb,nosql,relation,Mongodb,Nosql,Relation,是否有任何可能的方法列出Mongodb中模型之间的关系并删除或编辑它们 更新架构: 公司: 产品: { "name": "string", "qty": 0, "exp": "string", "id": "string" } 更新2: "relations": { "products": { "type": "hasMany", "model": "product", "foreignKey": "comp_id" }
{
"name": "string",
"qty": 0,
"exp": "string",
"id": "string"
}
更新2:
"relations": {
"products": {
"type": "hasMany",
"model": "product",
"foreignKey": "comp_id"
}
},
我仍然不能完全确定你想要完成什么。但是我有一个关于如何设计模式和添加外键的建议 如果我是你,我会像这样设计我的公司系列:
{
"_id" : ObjectId("5a291fd838f4fb1fc057bbbc"), // _id is automatically generated if you don't provide any
"name" : "ABC",
"tel" : "1234567"
}
{
"_id" : ObjectId("5a29208838f4fb1fc057bbbf"),
"name" : "DEF",
"tel" : "5556667"
}
这就是我如何设计我的产品系列:
{
"_id" : ObjectId("5a29211838f4fb1fc057bbc4"),
"name" : "ProductA",
"qty" : NumberInt(1),
"exp" : "exp1",
"companyIds" : [
ObjectId("5a291fd838f4fb1fc057bbbc"), // the foreign keys of the each company
ObjectId("5a29208838f4fb1fc057bbbf")
]
}
这样可以更容易地删除或更新您的收藏。您能更具体一点吗?您的数据库模式是什么样子的?到目前为止,您尝试了什么?我创建了错误的关系hasmany并通过环回属于关系,然后我从环回项目中删除了关系,但仍然得到错误,即没有为公司模型定义关系产品。公司和产品没有关系。你能用一些虚拟值粘贴你的模式吗?不要做评论,而是更新你的问题。“当你用一句话描述你的问题时,很难帮助你。”@AlexP。我已经更新了一些信息。那么你的两个收藏之间的联系在哪里呢?
{
"_id" : ObjectId("5a29211838f4fb1fc057bbc4"),
"name" : "ProductA",
"qty" : NumberInt(1),
"exp" : "exp1",
"companyIds" : [
ObjectId("5a291fd838f4fb1fc057bbbc"), // the foreign keys of the each company
ObjectId("5a29208838f4fb1fc057bbbf")
]
}