我正在寻找一个支持基于位置的搜索的正确数据库,发现MongoDB支持GeoJSON对象。
这是一个商店定位器应用程序,用户可以在其中环顾四周并选择离他最近的商店。
简单的供应商模式:
const VendorSchema = new mongoose.Schema({
vendorId: {
type: String,
required: true,
unique: true
},
address: {
type: String,
required: [true, 'Please add address']
},
formattedAddress: {
type: String
},
location: {
type: {
type: String,
enum: ['Point']
},
// GeoJSON Points
coordinates: {
type: [Number],
index: '2dsphere'
},
formattedAddress: String,
street: String,
city: String,
state: String,
zipcode: String,
country: String
},
createdAt: {
type: Date,
default: Date.now
}
});
数据库将具有许多供应商可以销售的快速消费品。
产品架构:
const ProductSchema = new Schema({
id: {
type: Number,
required: true,
unique: true
},
name: {
type: String,
required: true
},
desc: {
type: String,
required: true
},
price: Number,
createdAt: {
type: Date,
default: Date.now
}
});
由于这些产品可以由多个供应商以不同的价格出售(折扣后,所以供应商与产品之间将存在1-to-N
关系,而供应商与产品之间将具有N-to-1
关系-本质上是N-to-N
关系。
因此正在考虑创建新的架构,VendorProduct架构:
const VendorProductSchema = new Schema({
price: Number,
discountVal: Number, // Discount Value,
vendor : {
type: ObjectId,
ref: 'ProductImage'
},
product : {
type: ObjectId,
ref: 'ProductImage'
},
createdAt: {
type: Date,
default: Date.now
}
});
这变得棘手/具有挑战性:
用户可以在他们周围查找供应商/商店,也可以直接搜索产品。由于它是一项基于位置的服务-系统应该发送围绕用户的产品,因此考虑在VendorProduct模式中存储供应商的GeoJSON位置详细信息,以便系统只能使用供应商详细信息查询用户周围的产品。
带有GeoJSON详细信息的VendorProduct架构:
const VendorProductSchema = new Schema({
price: Number,
discountVal: Number, // Discount Value,
vendor : {
type: ObjectId,
ref: 'ProductImage'
},
product : {
type: ObjectId,
ref: 'ProductImage'
},
location: {
type: {
type: String,
enum: ['Point']
},
// GeoJSON Points
coordinates: {
type: [Number],
index: '2dsphere'
}
},
createdAt: {
type: Date,
default: Date.now
}
});
这只是要记住,单个产品可以由多个供应商出售。当用户搜索产品名称时-可能有N个以不同价格出售此产品的卖家。但是,为了只为该用户查询找到最接近的供应商,他们正在考虑在VendorProductSchema中针对产品存储供应商位置详细信息。
将从ProductSchema获取产品名称,描述,图像之类的产品信息,但价格详细信息将从最接近User的供应商的VendorProductSchema中获得。
一些问题:
我仅了解基本的MongoDB,但我注意到您将位置数据存储在VendorProduct集合中。在这种情况下,如果说卖主编辑他们的坐标,则需要更新多个文档。