最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我想使用限制从 mongodb 获取方法,我正在发送一个查询

网站源码admin42浏览0评论

我想使用限制从 mongodb 获取方法,我正在发送一个查询

我想使用限制从 mongodb 获取方法,我正在发送一个查询

其实我对mongodb的limit方法很困惑。我的代码应该只发送两家酒店的数据,我正在从后端查询请求。

export const getHotels = async (req, res, next) => {
    try {
        const hotels = await Hotel.find(req.query).limit(req.query.limit)
        res.status(200)
        res.json(hotels)
    } catch (err) {
        next(err)
    }
}

以上是我为查询请求编写的代码。 还有

localhost:8800/api/hotels?featured=true&limit=2

它是我的后端网址,它必须只向我发送 2 家酒店的数据

router.get('/countByType',countByType);

上面是我正在使用的router get方法

回答如下:

您的意思是在 Mongo 查询 find 方法中使用 limit 路由查询参数吗?

如果否,请尝试将代码更改为:

export const getHotels = async (req, res, next) => {
    try {
        const filter = {
            featured: req.query.featured
        };
        const hotels = await Hotel.find(filter).limit(req.query.limit)
        res.status(200)
        res.json(hotels)
    } catch (err) {
        next(err)
    }
}
发布评论

评论列表(0)

  1. 暂无评论