2017-03-09 1 views
-1

mongodb에서 여러 주소의 경우 일반적으로 배열에 주소를 저장합니다.mongodb에서 여러 주소에 대해 사용자 정의 주소 유형에 대한 스키마를 정의하는 방법

address:[ 
{ 
"type" : "home", 
"primary" : false,
        
"streetaddress" : "Some",
        
"locality" : "Farnborough", 
"region" : "England", 
"formatted" : "Farnborough, United Kingdom", 
"country" : "United Kingdom", 
"location" : [ 
-0.752615, 
51.2868939 
] 
}, 
{ 
"type" : "business",
       
"locality" : "London", 
"region" : "England", 
"formatted" : "The Gherkin, London, United Kingdom", 
"primary" : false, 
"streetaddress" : "30 St Mary Axe", 
"country" : "United Kingdom", 
"location" : [ 
-0.08030649999999999, 
51.51449179999999 
] 
} 
] 

하지만 키로 특정 주소를 가져 오려면 예. 가정 또는 비즈니스 환경에서는 루프를 통해 배열을 작성해야합니다. 어떤 다른 방법은 당신이 정확하게 배열의 특정 값을 조회하는거야 알고는 상기 값을 알고있는 경우,

{ 
     home_address: 
       { 
       "streetaddress" : "Some",
   
       "locality" : "Farnborough", 
       "region" : "England", 
       "formatted" : "Farnborough, United Kingdom", 
       "country" : "United Kingdom", 
       "location" : [ 
           -0.752615, 
           51.2868939 
          ] 
      }, 
    business_address : { 
       "locality" : "London", 
       "region" : "England", 
       "formatted" : "The Gherkin, London, United Kingdom", 
       "primary" : false, 
       "streetaddress" : "30 St Mary Axe", 
       "country" : "United Kingdom", 
       "location" : [ 
           -0.08030649999999999, 
           51.51449179999999 
          ], 
      }, 
    primary_address : "home_address" 

    } 
+0

더 보는 혜택을 할 수있다

는 "차"키 또는, 그것을 다른 방법을 넣어, 당신은에 의해 주소를 얻으려면 기준은 무엇인가? –

+0

업데이트 된 질문. 쿼리는이 시나리오에서 json 형식을 작성하는 방법과 관련이 있습니다. 여기서 주소 배열로 루핑하지 않고 "집"주소를 가져 오려고합니다. – jit

답변

0

일반적으로 다음과 같이 정의 이외 있는가, 그것은 좋은 것입니다 그 값을 키로 사용하여 하위 문서로 변환하십시오. 위에있는 것과 유사

:

"address": { 
    "type_home": { "street": ... }, 
    "type_business": {"street": ...}, 
    "primary": "type_home", 
    "available": ["type_home", "type_business"] 
} 

유연한 스키마는 MongoDB의 기능의 일부이며, 당신은 당신의 응용 프로그램 사용을 최적화 할 수 있도록하려면이 옵션을 사용합니다. 위의 스키마는 배열을 반복하는 것보다 특정 주소를 빠르게 쿼리하는 데 도움이됩니다. 당신은에 의해 무슨 뜻 이죠 data models examples

관련 문제