在 NodeJS 中使用 Mongoose 实现 MongoDB 关系
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
先决条件:
术语
模式
对数据单元应呈现的形状的描述。因此,对于房屋而言,重要的不是数据本身,而是对房屋数据应呈现形状的描述。
const mongoose = require("mongoose")
const houseSchema = new mongoose.Schema({
street: String,
city: String,
state: String,
zip: String
})
模式
如果我们想要管理一组这种数据类型的文档(一组项目),那么我们就需要声明一个模型。这将创建一个集合,并成为向该集合中添加、更新、删除和检索数据的通道。
const House = mongoose.model("House", houseSchema)
// query for all houses
House.find({})
一对一关系
一对一关系是最简单的。想象一下,每栋房子只能有一个主人,每个主人也只能拥有一栋房子。这就是一对一关系。双方的所有信息都是唯一的,因此实际上不需要多个集合。相反,我们可以将一种类型的数据嵌套在另一种类型中。
const mongoose = require("mongoose")
const Owner = new mongoose.Schema({
name: String
})
const houseSchema = new mongoose.Schema({
street: String,
city: String,
state: String,
zip: String
owner: Owner
})
const House = mongoose.model("House", houseSchema)
// Create a new house
House.create({
street: "100 Maple Street",
city: "Fort Townville",
state: "New West Virgota",
zip: "77777"
owner: {name: "Alex Merced"}
})
// query for all houses, will include the nested owner info
House.find({})
一对多
我们来看看如何重构这段代码,使其能够处理一个业主拥有多栋房屋,但每栋房屋只有一个业主的情况。这是一对多关系。因此,业主是关系中的“一”方,房屋是关系中的“多”方。通常,我们会从“多”方追踪“一”方(即通过房屋数据来追踪业主)。
在 Mongoose 中,我们有一种特殊的数据类型,它告诉 Mongoose 该字段中的所有条目都是其他集合中文档的对象 ID。请看下面的示例。
当我们查询数据时,填充函数会确保 mongoose 从相关表中获取数据,并在需要的地方插入数据。
注意:您也可以选择在 Owner 架构中嵌套 House 的 Arya,但是单个文档的大小有限制,如果您尝试嵌套过多数据,可能会导致以后出现扩展问题。
const mongoose = require("mongoose")
const ownerSchema = new mongoose.Schema({
name: String
})
const Owner = mongoose.model("Owner", ownerSchema)
const houseSchema = new mongoose.Schema({
street: String,
city: String,
state: String,
zip: String
owner: {type: mongoose.Types.ObjectId, ref: "Owner"}
})
const House = mongoose.model("House", houseSchema)
// Create a Owner
const alex = await Owner.create({name: "Alex Merced"})
// Create a new house
House.create({
street: "100 Maple Street",
city: "Fort Townville",
state: "New West Virgota",
zip: "77777"
owner: alex
})
// query for all houses, use populate to include owner info
House.find({}).populate("owner")
多对多
实际上,房屋可以有多位业主,业主也可以有多位业主,因此我们之间存在着真正的多对多关系。在这种情况下,我们需要创建第三个集合来跟踪不同的匹配项。
const mongoose = require("mongoose")
const ownerSchema = new mongoose.Schema({
name: String
})
const Owner = mongoose.model("Owner", ownerSchema)
const houseSchema = new mongoose.Schema({
street: String,
city: String,
state: String,
zip: String
})
const House = mongoose.model("House", houseSchema)
const houseOwnerSchema = {
owner: {type: mongoose.Types.ObjectId, ref: "Owner"},
house: {type: mongoose.Types.ObjectId, ref: "House"}
}
const HouseOwner = mongoose.model("HouseOwner", houseOwnerSchema)
// Create a Owner
const alex = await Owner.create({name: "Alex Merced"})
// Create a new house
const mapleStreet = await House.create({
street: "100 Maple Street",
city: "Fort Townville",
state: "New West Virgota",
zip: "77777"
owner: alex
})
// Create record that the owner owns the house
HouseOwner.create({owner: alex, house: mapleStreet})
// QUery for all houses owned by alex
HouseOwner.find({owner: alex}).populate("house")
//Query for all owners of the Maple Street House
HoseOwner.find({house: mapleStreet}).populate("owner")
结论
希望这对您在下一个应用程序中实现关系有所帮助。
文章来源:https://dev.to/alexmercedcoder/mongodb-relationships-using-mongoose-in-nodejs-54cc