Anuar
"title": "Toyota Camry XV75",
"description": "Toyota Camry XV75",
"images": [
"https:// ..."
],
"vinNumber": "12354ljk1h2b1234 ",
"mileage": "12341234",
"mainDamage": "-",
"secondaryDamage": "-",
"valuedPrice": 15000000,
"cylinders": 4,
"carcaseType": "2",
"color": "Серый",
"engineType": "2.5",
"transmission": "AUTOMATIC",
"driveUnit": "2WD",
"tsClassification": "AUTOMOBILE",
"fuelType": "Бензин",
"keys": 2,
"category": "66192d76fec73c6bc2e64bb6",
"type": "popular",
"properties": {},
"_id": "661927e5fec73c6bc2e64ba0"
}
вот такой запрос
Anuar
import { Car } from '@/models/Car'
import { mongooseConnect } from '@/lib/mongoose'
import { isAdminRequest } from '@/pages/api/auth/[...nextauth]'
export default async function handle(req, res) {
const { method } = req
await mongooseConnect()
await isAdminRequest(req, res)
if (method === 'GET') {
if (req.query?.id) {
res.json(await Car.findOne({ _id: req.query.id }))
} else {
res.json(await Car.find())
}
}
if (method === 'POST') {
const {
title,
type,
description,
images,
vinNumber,
mileage,
mainDamage,
secondaryDamage,
valuedPrice,
cylinders,
carcaseType,
color,
engineType,
transmission,
driveUnit,
tsClassification,
fuelType,
keys,
category,
properties
} = req.body
const carDoc = await Car.create({
title,
type,
description,
images,
vinNumber,
mileage,
mainDamage,
secondaryDamage,
valuedPrice,
cylinders,
carcaseType,
color,
engineType,
transmission,
driveUnit,
tsClassification,
fuelType,
keys,
category,
properties
})
res.json(carDoc)
}
if (method === 'PUT') {
const {
title,
type,
_id,
description,
images,
vinNumber,
mileage,
mainDamage,
secondaryDamage,
valuedPrice,
cylinders,
carcaseType,
color,
engineType,
transmission,
driveUnit,
tsClassification,
fuelType,
keys,
category,
properties
} = req.body
const carDoc = await Car.updateOne(
{ _id },
{
title,
type,
description,
images,
vinNumber,
mileage,
mainDamage,
secondaryDamage,
valuedPrice,
cylinders,
carcaseType,
color,
engineType,
transmission,
driveUnit,
tsClassification,
fuelType,
keys,
category,
properties
}
)
res.json(carDoc)
}
if (method === 'DELETE') {
if (req.query?.id) {
await Car.deleteOne({ _id: req.query?.id })
res.json(true)
}
}
}
Anuar
import { Car } from '@/models/Car'
import { mongooseConnect } from '@/lib/mongoose'
import { isAdminRequest } from '@/pages/api/auth/[...nextauth]'
export default async function handle(req, res) {
const { method } = req
await mongooseConnect()
await isAdminRequest(req, res)
if (method === 'GET') {
if (req.query?.id) {
res.json(await Car.findOne({ _id: req.query.id }))
} else {
res.json(await Car.find())
}
}
if (method === 'POST') {
const {
title,
type,
description,
images,
vinNumber,
mileage,
mainDamage,
secondaryDamage,
valuedPrice,
cylinders,
carcaseType,
color,
engineType,
transmission,
driveUnit,
tsClassification,
fuelType,
keys,
category,
properties
} = req.body
const carDoc = await Car.create({
title,
type,
description,
images,
vinNumber,
mileage,
mainDamage,
secondaryDamage,
valuedPrice,
cylinders,
carcaseType,
color,
engineType,
transmission,
driveUnit,
tsClassification,
fuelType,
keys,
category,
properties
})
res.json(carDoc)
}
if (method === 'PUT') {
const { _id } = req.body
const carDoc = await Car.updateOne({ _id }, { $set: req.body })
res.json(carDoc)
}
if (method === 'DELETE') {
if (req.query?.id) {
await Car.deleteOne({ _id: req.query?.id })
res.json(true)
}
}
}
Anuar
```
import mongoose, { model, Schema, models } from 'mongoose'
const CarSchema = new Schema(
{
title: { type: String, required: true },
description: String,
images: [{ type: String }],
vinNumber: String,
mileage: String,
mainDamage: String,
secondaryDamage: String,
valuedPrice: { type: Number, required: true },
cylinders: Number,
carcaseType: String,
color: String,
engineType: String,
transmission: String,
driveUnit: String,
tsClassification: String,
fuelType: String,
keys: Number,
type: { type: String, required: true },
properties: { type: Object },
category: { type: mongoose.Types.ObjectId, ref: 'Category' }
},
{
timestamps: true
}
)
export const Car = models.Car || model('Car', CarSchema)
```
Denis
Всем привет
Прошу не кидать камни, с mongo начал общаться вчера
Сейчас на моем проекте есть 1 менеджд кластер на яндексе, 1 жирный хост, там 15 бд, самая прожорливая 1 коллекий в 1 бд
Лид хочет ускорить запросы на чтение, и снизить нагрузку на хост, чтобы понизить класс (меньше цпу и озу)
Вариант с репликацией, который изначально сказал лид: доабвляем 2 реплики, по докам понял, что запросы все равно все обрабатывает primary, для того, чтобы на чтение летели к реплика указываем readPreference=secondaryPreferred (можно поймать рассинхрон)
Вариант с шардингом: 2 шарда юез реплик. Как я понимаю, обычные бд будут раскиданы по ним автоматически. В самой жирной бд включаю шардинг на самую большую коллекцию по нужному инедксу, и она уже будет раскидана по ним. При этом будет 2 хоста, как я понимаю, под данные и автоматом яндекс создаст MONGOS и MONGOCFG
Вопрос в том, поможет ли и какой лучше подходит из этих вариантов?
Первичная цель ускорить запросы на чтение, вторичная разбить 1 большой хост на несколько поменьше (сейчас 8 цпу 32 озу, понизить до 4 цпу, 16 озу)