#database #mongodb #query-optimization
Вопрос:
мой вопрос таков
connection.db.collection('hotels').aggregate([
{
"$match": {
"$text": {
"$search": hotelName
}
}
},
{
"$limit": skip limit
},
{
"$skip": skip
},
{
'$lookup': {
from: 'facilities',
localField: 'facilities',
foreignField: 'code',
as: 'facilities_doc'
}
},
{
'$lookup': {
from: 'hotelthemes',
localField: 'themes',
foreignField: 'code',
as: 'thems_doc'
}
},
{
'$lookup': {
from: 'hoteltypes',
localField: 'hotel_type',
foreignField: 'code',
as: 'hotel_type_doc'
}
},
{
'$lookup': {
from: 'regions',
localField: 'regions',
foreignField: 'code',
as: 'region_doc'
}
},
{
'$lookup': {
from: 'destinations',
localField: 'destination',
foreignField: 'code',
as: 'destination_doc'
}
},
{
'$lookup': {
from: 'countries',
localField: 'country',
foreignField: 'code',
as: 'country_doc'
}
}
]).toArray()
.then(result => {
console.log(result.length)
res.json(result)
}).catch(err => {
console.log(err)
res.json(err)
})
кроме того, я также делюсь образцом документа MongoDB
{
"address": "cassad 1017 ZA Amsterdam, Netherlands",
"availability_score": 0.00244531471157662,
"book_score": null,
"checkin_from": "14:00",
"checkin_to": "",
"checkout_from": "",
"checkout_to": "11:00",
"code": "123455",
"currencycode": "EUR",
"descriptions": {
"hotel_information": "<p><b>Property Location</b> <br />With a stay at Hotel Asterisk in Amsterdam (Museum Quarter), you'll be minutes from Heineken Experience and De Duif. This hotel is within close proximity of Albert Cuyp Market and Sarphatipark.</p><p><b>Rooms</b> <br />Make yourself at home in one of the 42 guestrooms. Complimentary wireless Internet access is available to keep you connected. Conveniences include phones, as well as safes and desks.</p><p><b>Rec, Spa, Premium Amenities</b> <br />Make use of convenient amenities such as complimentary wireless Internet access, gift shops/newsstands, and tour/ticket assistance.</p><p><b>Dining</b> <br />A complimentary buffet breakfast is served daily.</p><p><b>Business, Other Amenities</b> <br />Featured amenities include multilingual staff, luggage storage, and an elevator (lift).</p>"
},
"email": null,
"facilities": [
"122b",
"122d"
],
"gmt_offset": "UTC 02:00",
"hotel_score": 30.405815533466,
"name": "hotel name",
"nr_bars": null,
"nr_halls": null,
"nr_restaurants": null,
"nr_rooms": 42,
"phone": "567300323",
"stars": 2,
"updated_at": "2021-06-03T09:00:50.135980Z",
"year_built": null,
"zipcode": "44 ZA",
"images": [
{
"tag": "",
"category": "0f",
"original": "https://assets.cosmofds-data.com/1/7d/282993.jpg",
"room_category": null,
"thumbnail_images": {
"mid": "https://assets.cosmffos-data.com/thumbnails/mid/1/67a7d/282993.jpg",
"large": "https://assets.cosmos-data.com/thumbnails/large/1/004b8f8a824d64b70e06fd8170767a7d/282993.jpg",
"small": "https://assets.cosmos-data.com/thumbnails/small/1/004b8f8a824d64b70e06fd8170767a7d/282993.jpg"
}
},
{
"tag": "",
"category": "0f",
"original": "https://assets.cosmos-data.com/1/v/282993.jpg",
"room_category": null,
"thumbnail_images": {
"mid": "https://assets.cosmos-data.com/thumbnails/mid/1//282993.jpg",
"large": "https://assets.cosmos-data.com/thumbnails/large/1/4/282993.jpg",
"small": "https://assets.cosmos-data.com/thumbnails/small/1/4/282993.jpg"
}
}
],
"latitude": 32.3588347328288,
"longitude": 24.89386737346649,
"chains": [],
"themes": [
"1007",
"1009",
"1015"
],
"hotel_type": "24",
"master": null,
"country": "nl",
"regions": [
"10b5"
],
"destination": "278f"
}
Я сделал текстовый индекс в поле имя
обычный индекс в поле кода
что я должен добавить или сделать, чтобы сделать его оптимальным и эффективным.
у меня в общей сложности 650000 документов в этой коллекции
размер коллекции составляет 5,9 ГБ
запрос длится до 1 минуты, я хочу оптимизировать его в диапазоне 400 мс
Комментарии:
1. Я вижу, что в вашем запросе 6 поисковых запросов, это плохой признак вашего дизайна схемы, старайтесь избегать поисков/соединений в monfodb. попробуйте управлять 1/2 коллекциями вместо 6 коллекций.