首页 > 解决方案 > 带有 expressjs 的 Mongoose 多查询运算符

问题描述

我正在尝试学习如何制作 API。我遇到了一个超越我的问题。我的问题是关于数据库查询。这是我的终点;

/api/stats/:userId/:period/:type/:point?/

:userId完全是用户 ID(它是像 twitter 一样的雪花 ID)

:period总是有一个时间戳。(我稍后会调用它start_date

:type是用户操作。

:point?是一个可选参数,它意味着特定的用户操作。

所以,这是我的猫鼬模式;

id: String, // this is timestamp
userId: String,
point: String,
type: String

我需要一个包含所有这些参数的数据库查询。但有区别。它必须在一定的时间间隔内。所以,试试这个;

user.find({
    $and: [{ id: { $gte: start_date } }, { id: { $lte: end_date } }]
}).exec()

但是如何在这个查询中添加其他参数( userIdtype如果有)?point我搜索了一整天,但我找不到它。

标签: node.jsmongodbexpressmongooseapi-design

解决方案


推荐阅读