0

我有以下 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

我怎样才能实现这个?

4

1 回答 1

1

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

但是,如果您愿意相信它,这里有一个使用搜索查询对集合执行查找的快速路由示例:

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

query-to-mongo 解析器还使用offsetlimit处理对结果的分页。

于 2019-03-13T00:28:32.940 回答