我有三个公寓,建筑和basic_amenities 的文件。我想检索公寓basic_amenities的数据。通常公寓位于建筑物内,因此建筑和公寓的基本相同。使用这三个文件我如何检索公寓基本_amenities的数据?这是我的代码
db.property.find({"building.apartment_id" : "a1"}),
db.property.find({"basic_amenities.property_id" : "b2"});
这个查询是对的吗?
这是我的文件
公寓:
{
"_id" : "a1",
"apartment" : {
"sale_type" : "rental",
"owner_name " : "ram sunder",
"address" : {
"street" : "alphornstrasse",
"plot_no" : 54,
"city" : "mannheim",
"state" : "baden württemberg",
"country" : "germany",
"postal_code" : 68169,
"email" : " ram.sunder@gmail.com"
},
"total_area" : "120 sq meters",
"rental_price(EUR)" : {
"monthly" : 700,
"available_date" : ISODate("2005-03-15T23:00:00.000Z"),
"deposit_amount" : 2100
},
"features" : {
"floor" : 3,
"rooms" : 2,
"kitchen" : 1,
"bathroom" : 1,
"heating" : true,
"garden" : true,
"furnished" : true
},
"kid_condition" : "one kid"
}
}
Building:
{
"_id" : "b2",
"building" : {
"building_name" : "sai durga",
"available_apartments" : {
"1bhk" : 3,
"2bhk" : 4
},
"owner_name" : "umang dosi",
"address" : {
"street" : "alphornstrasse",
"plot_no" : 54,
"city" : "mannheim",
"state" : "baden württemberg",
"country" : "germany",
"postal_code" : 68169,
"email" : "umang123@gmail.com"
},
"total_area" : "1100 sq meters",
"apartment_id" : [
"a2",
"a1"
],
"features" : {
"no_of_apartments" : 64,
"community_hall" : 1,
"garden" : 2,
"office_room" : 1,
"swimming_pool" : true,
"security_cameras" : true,
"parking" : true,
"play_ground" : false
}
}
}
Basic_amenities:
{
"_id" : "ba-b2",
"basic_amenities" : {
"property_id" : "b2",
"hospital " : 10,
"restaurant" : 3,
"airport" : 56,
"bahn_station" : 5,
"city_center" : 6,
"public_transp_type" : [
"bus",
"tram",
"train"
],
"future_activity" : "constructing kpmg software company"
}
}
谁能告诉我如何查询这个?
此致 斯里卡特
答案 0 :(得分:0)
我认为您的文档过于规范化,并且在一个集合中组织不当。将公寓和建筑物分别分为aparments
和buildings
集合。您还可以展平文档的结构,因为它将是多余的:您不需要将_id
文档的所有非apartments
字段包装在apartment
嵌入文档中。 basic_amenities
是建筑物的财产,因此将设施存放为建筑物的一部分:
{
"_id" : "b2",
"building_name" : "sai durga",
"available_apartments" : {
"1bhk" : 3,
"2bhk" : 4
},
"owner_name" : "umang dosi",
"address" : {
"street" : "alphornstrasse",
"plot_no" : 54,
"city" : "mannheim",
"state" : "baden württemberg",
"country" : "germany",
"postal_code" : 68169,
"email" : "umang123@gmail.com"
},
"total_area" : "1100 sq meters",
"apartment_id" : [
"a2",
"a1"
],
"features" : {
"no_of_apartments" : 64,
"community_hall" : 1,
"garden" : 2,
"office_room" : 1,
"swimming_pool" : true,
"security_cameras" : true,
"parking" : true,
"play_ground" : false
},
"basic_amenities" : {
"property_id" : "b2",
"hospital " : 10,
"restaurant" : 3,
"airport" : 56,
"bahn_station" : 5,
"city_center" : 6,
"public_transp_type" : [
"bus",
"tram",
"train"
],
"future_activity" : "constructing kpmg software company"
}
}
在公寓文件中加入对包含建筑物的引用:
{
"_id" : "a1",
"building_id" : "b2",
"sale_type" : "rental",
"owner_name " : "ram sunder",
....
}
使用该参考资料查找公寓的设施,这些设施正是其内置建筑的设施:
function getAptAmenities(apt_doc) {
bldg = db.buildings.findOne({ "_id" : apt_doc.building_id })
if (bldg === null) return null
return bldg.basic_amenities
}