Mongo文档查询

时间:2017-02-23 12:17:30

标签: mongodb

我刚刚进入NoSQL数据库,特别是MongoDB。我使用SQL多年,所以我习惯了更传统的方法。

我有一个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": "xxx@xxx.co.uk",
"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"]
  }
]
 }

1 个答案:

答案 0 :(得分:1)

@RudyVerboven为你的问题答案提供了一个非常好的链接,但我想补充一点。

我建议您使用嵌入式文档,因为这是MongoDB执行得非常好的事情之一。因为在你提到的问题中你是否会单独收集你将依赖连接但是与SQL数据库不同,MongoDB不支持连接其他单一类型的连接,这是左外连接,也是在MongoDB 3.2版中添加的。

此外,MongoDB中的聚合操作使得嵌入式文档的处理非常容易,因为展开等功能可以将嵌套文档作为单独的文档,就像在您的示例中一样,您可以展开您的房间数组来制作单独的文档,如果需要,您可以使用 out 功能创建单独的文档集合。除此之外,您还可以使用索引嵌入字段来更快地进行查询,并且还有许多其他优点。据我所知MongoDb旨在使用同一集合中的大块嵌入文档进行最佳优化。