在 MongoDB 中存储(复杂)MongoDB 查询

发布于 2024-11-28 12:17:26 字数 727 浏览 3 评论 0原文

我有一个 MongoDB 文档集合,每个文档代表一条。 然后,我的应用程序的用户可以通过在鱼集合上创建查询来定义鱼的类型。这个查询可能非常复杂,使用条件运算符等。

例如,用户可以定义 “高度异常的类似鲨鱼的鱼” 是由以下返回的任何鱼:

{'length':{$gte : 45}, 'name' : {$in : ['Klaus', 'Alistair', 'Steve']}}

但是经常会发现新的鱼,我需要根据用户创建的查询为它们分配类型。也就是说,我以后需要多次再次使用这个查询。因此,我的想法是拥有一个包含如下文档的集合:

{'typename' : 'highly abnormal shark-like fish', 
'query' : '{'length':{$gte : 45}, 'name' : {$in : ['Klaus', 'Alistair', 'Steve']}}'}

我的问题是:我应该将查询存储为字符串吗?这是最好的方法吗?

请记住,当发现新鱼时,我必须使用 PHP 驱动程序应用查询。我应该将查询的 PHP 数组版本存储为字符串,然后使用 eval() 吗?

I have a MongoDB collection of documents, each document representing a fish.
Users of my application can then define a type of fish, by creating a query on the fish collection. This query can be very complex, using Conditional Operators, etc.

For example, a user could define the
'highly abnormal shark-like fish'
to be any fish returned by:

{'length':{$gte : 45}, 'name' : {$in : ['Klaus', 'Alistair', 'Steve']}}

But new fish are frequently being discovered, and I need to assign types to them based on the queries created by the users. That is, I will need to use this query again many times in the future. Therefore, my thought is to have a collection with documents like this:

{'typename' : 'highly abnormal shark-like fish', 
'query' : '{'length':{$gte : 45}, 'name' : {$in : ['Klaus', 'Alistair', 'Steve']}}'}

My question is: should I store the query as a string? Is that the best way?

Please keep in mind that as new fish are discovered, I will have to apply the query using the PHP driver. Should I store the PHP-array-version of the query as a string, then use eval()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-12-05 12:17:26

如果用户将查询构建为输入字符串,那么当然要存储它。您希望他们稍后能够对其进行编辑。但你已经进行了eval,不是吗?

否则序列化查询文档并存储它。

If users build the queries as an input string, sure, store that. You want them to be able to edit it later. But then you already do eval, don't you?

Otherwise serialize the query document and store that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文