我正在设计一个MongoDB集合并遇到了一个设计问题。
该集合保持两点之间的距离。它目前设置如下。
{
"_id" : ObjectId("5a7dbc6f16eac41778ae8255"),
"start" : 122,
"end" : 156,
"routes" : [
{
"routeType" : 1,
"Distance" : 455.0
},
{
"routeType" : 4,
"Distance" : 455.0
}
]
}
所以,我可以搜索开始和结束值。问题是"开始"可能是156和"结束" 122.用户可以搜索任何一种组合。
有更好的设计吗?
答案 0 :(得分:0)
如此设计
{
"_id" : ObjectId("5a7dbc6f16eac41778ae8255"),
"places" : [
122,
156
],
"routes" : [
{
"routeType" : 1.0,
"Distance" : 455.0
},
{
"routeType" : 4.0,
"Distance" : 455.0
}
]
}
你不关心什么是起点,什么是终点。只需将它们包裹在places
中,并确保places
有2分。搜索时
db.getCollection('yourtable').find({places: 122, places: 156})
和
db.getCollection('yourtable').find({places: 156, places: 122})
他们都给你你想要的相同结果