2017-02-23 135 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旨在通过最佳优化在同一个集合中处理大量嵌入文档。