2017-02-23 99 views
0

我剛剛進入NoSQL數據庫,特別是MongoDB。我與SQL一起工作了很多年,所以我習慣了更傳統的方法。Mongo文檔查詢

我有一個Json包,用於存儲我想要爲公寓收集的所有細節。我想我的問題是,我將這一切全部存儲爲一個記錄(文檔),還是應該將它分解爲不同的集合/文檔並將它們組合在一起進行一些操作。

這裏是我的JSON

{ 
    "Title": "First Class 8 bedroom home- resort-Exceptional decor and furnishings", 
    "Description" : "The Address is an eight bedroom, five bathroom Orlando vacation rental that’s located at the Champions Gate Resort – just a few minutes from the main gates of Walt Disney World!/n When you stay on property at Champions Gate, you are privy to a variety of wonderful amenities that include but aren’t limited to, a large community pool, fitness center, club house, sports courts and two championship golf courses./n The resort amenities aren’t the only draw for this property because the vacation home alone is one to write home about./n The Address has everything that you could want and more when going on vacation. Why stay at an over-crowded hotel when you can spread out and relax inside of your own private home?/n This property didn’t leave any stones unturned because every room, every bit of décor was placed with care./n Just take the open-concept main living, dining and kitchen area. The cool, light blue wall connects all three spaces into one complete area. The blue is perfectly complemented by the whites and grays which make the first floor feel even more open!/n The kitchen is fully-equipped and outfitted with stainless steel appliances and granite countertops and a five-person breakfast bar./n Next to the kitchen is your main living area with three comfortable, leather couches and a large flat-screen TV./n Your dining room is also in this space and has a 12-person dining table which proves to be the perfect place to enjoy a family meal with your loved ones!/n Right off of this area, through a sliding glass door, is your private pool area. It’s here that you can enjoy the Orlando weather as you swim around in your private pool or soak in your Jacuzzi. The covered lanai will protect you and your guests from any unwanted bugs!/n Vacations are meant to be fun, right? Well, when you’re not at the nearby Orlando attractions like, Walt Disney World, Universal Studios or Sea World, or even splashing around in your own private pool or the resort’s community pool, you can keep the fun going in your games room!", 
    "images": [ 
"https://imagesus-ssl.homeaway.com/mda01/3381ed5a-f882-4efa-b246-b94f6de9618f.1.6", 
"https://imagesus-ssl.homeaway.com/mda01/85a33780-4710-4815-b798-c6b6e6d0acc0.1.6", 
"https://imagesus-ssl.homeaway.com/mda01/1caf0dab-9d78-45ae-910b-d4e7088d88cf.1.6", 
"https://imagesus-ssl.homeaway.com/mda01/4123137e-086e-4171-9aad-d6aae14a7052.1.6" 
], 
    "contact": { 
"name": "James Privett", 
"emailAddress": "[email protected]", 
"skype": "james.privett", 
"phone": "xxxxx xxxx" 
}, 
"location": { 
"name": "Orlando", 
"latitude": 28.36433219, 
"longitude": -81.55729782 
}, 
"rooms" : [ 
    { 
    "title": "Bedroom 1", 
    "image": "https://imagesus-ssl.homeaway.com/mda01/3381ed5a-f882-4efa-b246-b94f6de9618f.1.6", 
    "description": " <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ut aliquip ex ea commodo consequat.</p> ", 
    "amenities": ["laundry","security","air conditioning","tv","satelite","open fire","swimming pool"] 
},{ 
    "title": "Bedroom 2", 
    "image": "https://imagesus-ssl.homeaway.com/mda01/85a33780-4710-4815-b798-c6b6e6d0acc0.1.6", 
    "description": " <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ut aliquip ex ea commodo consequat.</p> ", 
    "amenities": ["laundry","security","air conditioning","tv","satelite","open fire","swimming pool"] 
},{ 
    "title": "Bedroom 3", 
    "image": "https://imagesus-ssl.homeaway.com/mda01/1caf0dab-9d78-45ae-910b-d4e7088d88cf.1.6", 
    "description": " <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ut aliquip ex ea commodo consequat.</p> ", 
    "amenities": ["laundry","security","air conditioning","tv","satelite","open fire","swimming pool"] 
    } 
] 
} 
+0

您可以使用嵌入式文檔或選擇實現多個集合。這個線程肯定會回答你的問題:http://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – RudyVerboven

回答

1

@RudyVerboven給了一個很好的鏈接,你的問題的答案,但我想補充一些積分的例子。

我建議你使用嵌入式文檔,因爲這是MongoDB執行得很好的事情之一。由於在你提到的問題中,如果你將要單獨收集,你將依賴於連接,但與SQL數據庫不同,MongoDB不支持加入其他單一類型的連接,它是在MongoDB 3.2版本中添加的左外部等連接。

而且,在MongoDB的集聚操作使帶有嵌入文檔時很容易由於像開卷的功能,它可以使嵌套文檔作爲一個單獨的文件就像在你的榜樣,你可以放鬆你的房間陣盡出的單獨文件它和如果需要可以使用函數創建它們的單獨集合。除此之外,您還可以使用索引嵌入式字段更快地進行查詢,並且還有更多的優勢。根據我的說法,MongoDb旨在通過最佳優化在同一個集合中處理大量嵌入文檔。