npm 包,用于从 URL 查询构建 mongo 查询



我有以下 mongo 文档:

[{
"name": "Robert",
"title": "The art of war",
"description": "The art of war in the 20yh century"
},
{
"name": "Claadius",
"title": "The spring is back",
"description": "I love spring and all the seasons"
}
]

在我的 GET 方法上,我有一个查询来单独对 1 个属性、2 个或 3 个属性执行搜索。请参阅示例:?name=Robert&title=war&description=spring

我该如何实现?

这几乎正是查询到mongo的目的!它将您显示的查询转换为可以传递到 mongo 查找中的 mongo搜索条件。它处理一堆额外的搜索运算符(如>= 和 !=(,这就是它变得复杂的地方。

但是,如果您愿意信任它,下面是使用搜索查询对集合执行查找的快速路由示例:

https://gist.github.com/pbatey/20d99ff772c29146897834d0f44d1c29

查询到mongo解析器还处理分页到具有偏移量和限制的结果。

最新更新